Previously I did Salesforce integration with PHP. This time due to an office requirement I worked to integrate new CRM with a new programming language. It will be Microsoft Dynamic CRM 2013 with Java. I use Java Spring Framework, hence used spring in this blog.  I’m starting from scratch assuming you don’t have any MS Dynamic Account,  however I assume you have Spring Framework Basic knowledge.

It took me some time to properly integrate MS Dynamics 2013 due to the fact, till now there are not many integration documentations for MS Dyamics CRM 2013. Most of them are for MS Dynamics CRM 2011. Though MSDN SDK 2013 exists, but I found no Java or PHP documentation, it majority contains C#, JS and some other languages.  I tried a number of documentation/tutorials with different Java libraries but failed. I listed down the steps I did for making MS Dynamics CRM 2013 work,  so that other developers might find it helpful. Some of the below information can be found in MSDN ( Microsoft’s documentation), however some things that did to make the project work are also provided below.

What kind of MS Dynamic CRM ?

Microsoft provides two kinds of CRM installation: online version and inhouse installation. The authentication process when connecting to these CRM version is different. Online version uses claim-based authentication and other version uses active directory. In the first segment we will use MS Dynamics CRM online version and last segment contains things needed to be done for make MS Dynamics CRM 2013 (in house) version to work.

Getting Microsoft Documentation:

1. Visit

2. Download Microsoft Dynamics CRM 2011 Software Development Kit (SDK) from

3. Run the installer, it will unpack a list of files in SDK folder.

Java Spring Basic

Stated earlier, I assumed you have Spring’s Basic Knowledge. Spring is the most popular JAVA MVC Framework for developing web application. Anyway, if you have not worked with Spring earlier, this tutorial form tutorial point will help you:

Developer Account?

Microsoft does not give free developer account for MS Dyanamics CRM online. Microsoft Dynamics Certified Software Advisor (CSA) partners may qualify to receive a 12-month development account of Microsoft Dynamics CRM Online. That thats a length process. More information can be found here:

The only way to use online version is 30 day trial account. You can use this one to get started and meanwhile get your client company’s credentials to proceed next.

Phone number is a must when getting trial one The call or text (SMS) to give the code.

Setting up the CRM

1. Signup for CRM trial account browsing This account will be an Office 365 with CRM access.

2. When signed up, it will take some time for the system to setup the CRM. Once signed up, you will provided with a friendly name and instance url.  Instance URL might be inform of (Mine was  This is the URL To sign in directly to your Microsoft Dynamics CRM Online organization in the future. Going forward with next will help you to configure the CRM. Later-on, launch your CRM.

Setting up the project in Netbeans

The first setup to setup the java environment. Download and install JDK, tomcat and Netbeans (preferrable with all language support).  I used Java SE SDK 1.7 , tomcat 6 (I know its old but due to some project dependencies  haven’t changed it), Netbeans 7.3. You can also install XAMPP as it makes the webserver kind of complete. Latest XAMPP has tomcat within itself.

1. Create a java web application project named “SpringWithMSCRM2013”. From Netbeans File ->Create Project -> Java Web -> Web Application. Provide name SpringWithMSCRM2013 and click next. Select tomcat server with other fields default and click next. Tick Spring Web MVC framework and click finish button.

2. Download axis 1.6.1 from Unzip it to C:\ drive. Axis 1.6.1 is very important as other version might have difficulties. I tried 1.6.2 and Axis 1.5.4  earlier and failed.

3. Download HTTP Component client 4.1.3 from Unzip to C:\

4. Create a folder in SpringWithMSCRM2013 project named lib.

5.  Browse lib folder c:\axis-1.6.1 . Copy all the jar files and paste it in “SpringWithMSCRM2013” project’s lib folder (C:\xampp\htdocs\SpringWithMSCRM2013\lib).

6. Browse lib folder c:\httpcomponents-client-4.1.3. Copy all the jar files and paste it in “SpringWithMSCRM2013” project’s lib folder (C:\xampp\htdocs\SpringWithMSCRM2013\lib).

7. Add the jar files in the project.  In Netbeans project explorer window, expand “SpringWithMSCRM2013”.  Right click on Libraries folder, and select add/jar folder and finally select all the jar files in lib folder.

8. Check whether 3 main libraries are included: “activation-1.1.jar”, “log4j-1.2.15.jar” and “httpclient-4.1.1.jar”.

Creating Type Classes to be used for web Services

1. Find the your CRM’s required information and URL for web connectivity. Browse and login to CRM. Then click in the menu: CRM -> Settings -> Customizations. Click Developer Resources. This page will contain organization name, discovery service URL and organization service URL.


Customization_Microsoft Dynamics CRM

2. In Netbeans Project Explorer, under Source Packages create 3 folders: com.springwithmscrm2013.controller, com.springwithmscrm2013.helper and com.springwithmscrm2013.service.

3. Open command prompt in windows

4. Move to axis bin folder:

cd c:\axis2-1.6.1\bin

5.  set Java home with your JDK directory :

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_17".

6. Close command prompt and open it again moving to axis bin folder.

7. Use WSDL2Java program in axis 2 to create  type classes (just replace your username with tzulkarnine): 1st one using discovery url, 2nd one using instance name and  organization service url. Infact using another java tool/framework Maven or configuring spring with axis2 you can automatically create the type classes, however for simplicity I’m creating it directly.

C:\axis2-1.6.1\bin>WSDL2Java -uri https://disco.crm.dyna -p -s -o c:\xam
C:\axis2-1.6.1\bin>WSDL2Java -uri https://tzulkarnine.ap -p -s -o c:\xampp\htdocs\SpringWithMSCRM2013

8. Verify the create files: If you browse your project “SpringWithMSCRM2013″‘s source files, you will see a number of java files.


9. Replace in project “” with “services”.

10. Update the Spring configuration files:

a. WEB-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" antiJARLocking="true" path="/SpringWithMSCRM2013"/>

b. Dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <mvc:annotation-driven />
    <context:component-scan base-package="com.springwithmscrm2013" />
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
        <property name="suffix">


c. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">




d. ApplicationContext.xml (add logging if you need to log anything)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <!--bean id="propertyConfigurer"
          p:location="/WEB-INF/" />

<bean id="dataSource"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean id="logging" class="com.springwithmscrm2013.service.Logging" />

Setting up project environment For CRM web service usage

The microsoft’s document and code will be helpful here.

1. Open explore and move to SDK\Walkthroughs\Java2CRM\Java2CRMCRUD\src\java2crmpack folder.

2. Copy all of the java files except and paste it in your “SpringWithMSCRM2013” project’s source helper folder (NavCRM\src\java\helper). Also you can also drag it to source folder in netbeans.

3. Copy to controller folder and rename it IndexController.

4. Replace java2crmpac in all the files to  helper. From netbeans, use replace in project (in Edit Menu).

5.  Replace the lines in IndexController having javamscrm2013 package with the below , add annotation and :

package com.springwithmscrm2013.controller;

import java.rmi.RemoteException;
import java.util.UUID;

import com.springwithmscrm2013.helper.DeviceRegistrationFailedException;
import com.springwithmscrm2013.service.DiscoveryServiceStub;
import com.springwithmscrm2013.service.DiscoveryServiceStub.EndpointType;
import com.springwithmscrm2013.service.DiscoveryServiceStub.KeyValuePairOfEndpointTypestringztYlk6OT;
import com.springwithmscrm2013.service.DiscoveryServiceStub.OrganizationDetail;
import com.springwithmscrm2013.service.DiscoveryServiceStub.RetrieveOrganizationResponse;
import com.springwithmscrm2013.service.IDiscoveryService_Execute_DiscoveryServiceFaultFault_FaultMessage;
import com.springwithmscrm2013.service.IOrganizationService_Create_OrganizationServiceFaultFault_FaultMessage;
import com.springwithmscrm2013.service.IOrganizationService_Delete_OrganizationServiceFaultFault_FaultMessage;
import com.springwithmscrm2013.service.IOrganizationService_Retrieve_OrganizationServiceFaultFault_FaultMessage;
import com.springwithmscrm2013.service.IOrganizationService_Update_OrganizationServiceFaultFault_FaultMessage;
import com.springwithmscrm2013.helper.OnlineAuthenticationPolicy;
import com.springwithmscrm2013.service.OrganizationServiceStub;
import com.springwithmscrm2013.service.OrganizationServiceStub.Entity;
import com.springwithmscrm2013.helper.RequestDateTimeData;
import com.springwithmscrm2013.helper.SecurityData;
import com.springwithmscrm2013.helper.WsdlTokenManager;
import com.springwithmscrm2013.service.Logging;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import javax.wsdl.WSDLException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.springframework.beans.factory.annotation.Autowired;
import org.xml.sax.SAXException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;

public class IndexController {

    static Logger logger = Logger.getLogger(IndexController.class.getName());
    Logging logging;
    ServletContext servletContext=null;
    static String appPath;

3.  Replace the declaration of main function in IndexController:

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String Index(Map<String, Object> model, HttpServletRequest request) {

            System.setProperty("appPath", servletContext.getRealPath("/"));

            Map<String,Object> testIntegration = this.IndexHelper();
            logger.log(Priority.INFO, "Came here");
            //String workingDirectory = System.getProperty("user.dir");
            //logging.publish(IndexController.class.getName(), Level.OFF, "Working Directory: "+workingDirectory);

      } catch(IOrganizationService_Delete_OrganizationServiceFaultFault_FaultMessage ex){

      } catch( IDiscoveryService_Execute_DiscoveryServiceFaultFault_FaultMessage ex){

      }  catch(XMLStreamException ex){

      }  catch(IOrganizationService_Create_OrganizationServiceFaultFault_FaultMessage ex){

      }  catch( IOrganizationService_Retrieve_OrganizationServiceFaultFault_FaultMessage ex){

      }  catch(IOrganizationService_Update_OrganizationServiceFaultFault_FaultMessage ex){

        return "index";

4. Update variables with you CRM’s credentials (user name, password and service url) in When you signed up for the trial account, you selected the username and password.

     * Microsoft account (e.g. or Microsoft Office 365 (Org ID e.g. User Name.
    private static final String UserName = "";
     * Microsoft account or Microsoft Office 365 (Org ID) Password.
    private static final String UserPassword = "password";
     * Unique Name of the organization
    private static final String OrganizationUniqueName = "tzulkarnine";

5. Rename the main function to indexHelper and also change the return type.

    public Map<String,Object> IndexHelper() throws IOrganizationService_Delete_OrganizationServiceFaultFault_FaultMessage, IDiscoveryService_Execute_DiscoveryServiceFaultFault_FaultMessage, XMLStreamException, IOrganizationService_Create_OrganizationServiceFaultFault_FaultMessage, IOrganizationService_Retrieve_OrganizationServiceFaultFault_FaultMessage, IOrganizationService_Update_OrganizationServiceFaultFault_FaultMessage{
        try {

        // Retrieve the authentication policy for the discovery service.
        OnlineAuthenticationPolicy discoveryPolicy =
                new OnlineAuthenticationPolicy(DiscoveryServiceURL + FlatWSDLSuffix);
        WsdlTokenManager discoeryTokenManager = new WsdlTokenManager();        
        // Authenticate the user using the discovery authentication policy.
        SecurityData discoverySecurityData = discoeryTokenManager.authenticate(DiscoveryServiceURL, 

        // Retrieve discovery stub using organization URL with the security data.
        DiscoveryServiceStub discoveryServiceStub = createDiscoveryServiceStub(DiscoveryServiceURL, 

        // Retrieve organization service url using discovery stub.
        String orgUrl = discoverOrganizationUrl(discoveryServiceStub, OrganizationUniqueName);

        // The discovery service stub cannot be reused against the organization service 
        // as the Issuer and AppliesTo may differ between the discovery and organization com.springwithmscrm2013.service.
        // Retrieve the authentication policy for the organization service.
        OnlineAuthenticationPolicy organizationPolicy =
                new OnlineAuthenticationPolicy(orgUrl + FlatWSDLSuffix);
        WsdlTokenManager orgTokenManager = new WsdlTokenManager();        
        // Authenticate the user using the organization authentication policy.
        SecurityData securityData = orgTokenManager.authenticate(orgUrl, 

        // Retrieve organization stub using organization URL with the security data.
        OrganizationServiceStub serviceStub = createOrganizationServiceStub(

        // Create an sample account record.
        OrganizationServiceStub.Guid newAccountGUID = createAccount(serviceStub);

        // Retrieve the sample account record.
        readAccount(serviceStub, newAccountGUID);

        // Update the sample account record.
        updateAccount(serviceStub, newAccountGUID);

        // Retrieve updated sample account record.
        readAccount(serviceStub, newAccountGUID);
        Scanner scanner = new Scanner(;
        System.out.println("\nDo you want this sample account record deleted? (y/n) [y]: ");
        String answer = scanner.nextLine();

        if(!answer.startsWith("n") || !answer.startsWith("N")){
            // Delete the sample account record.
            deleteAccount(serviceStub, newAccountGUID);
        } catch (IllegalStateException e) {
        } catch (SAXException e) {
        } catch (ParserConfigurationException e) {
        } catch (DeviceRegistrationFailedException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        } catch (URISyntaxException e) {
        } catch (WSDLException e) {
        return new HashMap<String,Object>();

6. Update getConfigurationContext() so that it can point properly to axis2.xml

private static ConfigurationContext getConfigurationContext() throws AxisFault {

        String workingDirectory = System.getProperty("appPath");

        String fileSeperator = System.getProperty("file.separator");
        String pathToAxis2File = workingDirectory + fileSeperator + "WEB-INF" + fileSeperator + "axis2.xml";

        logger.debug("Working directory: " + workingDirectory);
        logger.debug("Path to Axis2.xml file: " + pathToAxis2File);

        ConfigurationContext ctx;
        try {
            ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(workingDirectory, pathToAxis2File);
        } catch (AxisFault e) {
            throw e;
        return ctx;

7.  Navigate to in helper folder. Update the access privilege of getRequestDateTime() function:

    public static RequestDateTimeData getRequestDateTime()

6. Now build (Shift + F11) and run (F6) the application.

Changes for Inhouse Version of MS Dynamics CRM (on premise)

Here begins the hardest part of the integration. In order to connect to on-premise CRM, we need active directory + claim based authentication. However,  Microsoft only supports .NET  languages for this kind of authentication. So apparently you wont be able to connect with on premise MS Dynamics CRM with java.  If you want to do so, you will get an policy regarding exception as because the app tries to connect to get authenticated. If  you are an advanced java developer, you can go ahead and try to replicate the .NET stuff in java studying soap envelops. However, I don’t support that. In one earlier occasion, our developers did that and it broke after a CRM update and they could not figure out the problem. Hence, the solution might be fragile and if broken there might not that many options to fix it.

As .NET is supported by Microsoft,  the solution I choose what developing a .NET application with web service which can deliver the data to other web applications.  Hence, in the java application we java generate classes using wsdl version of .NET application using wsdl2java. However, if you have a windows server you can follow the steps.  Below is the steps that I took to create the .NET app:

1. Browse SDK folder, and copy WebAppWalkthrough folder in Walkthroughs\Portal\ folder to you desired location.

2. Activate IIS. In windows 8, tap windows key, search “windows feature” and in setting you find “turn windows feature on or off”. Click that and run on the IIS.

3. Download visual web developer 2012 and install it.

4.  Open the WebAppWalkthrough in visual web developer and rename it to what you want. I renamed it “DSSCRMApp”.  Find all “WebAppWalkthrough” in files to “DSSCRMApp”.

5. Generate Xrm.cs using CrmSvcUtil. Generally the project contains a Xrm.cs, however it contains all generic fields of entity. So if you have custom fields in entities, please generate a new one and replace it.

a. Browse bin folder in SDK in command prompt.

b. run command updating with your crmserver address, organization name , active directory domain, username and password.

CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm.cs /url:http://<crmserver>/<crm-org>/XRMServices/2011/Organization.svc /domain:<domain> /username:<admin> /password:<password> /namespace:Xrm /serviceContextName:XrmServiceContext /serviceContextPrefix:Xrm

c. Delete old Xrm.cs from project and copy the new Xrm.cs from bin folder to your project folder.

6.  Update Web.config file with your CRM information as above:

    <add name="Xrm" connectionString="Server=<crmserver>/<crm-org>; Domain=<domain>; Username=<admin>; Password=<password>  />

6. Build and run the project. View it in browser : Ctrl + Shift + W. Hopefully it will connect and you will be able to browse the data in web application by browsing some of the links.

7. Now, we come to the web service development:


1. If you are getting a number of errors during build, this is probably due to having a wrong version of axis. As we are using the MS documentation code, we will have to use 1.5.4 version to make the generic classes.

2. If build is ok and after run you get null pointer exception, please follow the below instructions.

a. Check whether you are getting the cipher value from device credentials soap response. Browse LiveIDManager and look for cipher value.

System.out.println("CipherValue: " + cipherValue);

b. Check whether the provided credentials are ok from the response.  Browse LiveIDManager and print the response after getSOAPResponse for security token.

System.out.println("Security Token Response: " + securityTokenXML);

4. Getting error regaring wsx namespace:

com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "wsx"

Do this:

  • Proposed solution: edit the generated Organization Service stub “”, search and replace:
    • wsx:MetadataReference by MetadataReference
    • wsx:MetadataSection by MetadataSection

3. If you are getting below error:

[ERROR] The system is attempting to engage a module that is not available: addressing

Then you can do:

a.  rename addressing mar file in lib directory to jar file.

c:\xampp\htdocs\SpringWithMSCRM2013\lib>rename addressing-1.6.1.mar addressing-1

b. Comment module ref in axis2.xml :

<!-- <module ref="addressing"/> -->


Leave a Reply

Your email address will not be published.