Accessing Microsoft Dynamics CRM Web Services

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

It is possible to access the Microsoft Dynamics CRM Web services using asynchronous JavaScript and XML (AJAX) from your client-side JScript code. Several new features have been included in Microsoft Dynamics CRM 4.0 to help support this. These new capabilities are targeted for customizations in Microsoft Dynamics CRM Online.

In the on-premise version of Microsoft Dynamics CRM 4.0 the most efficient way to access Microsoft Dynamics CRM Web services is to create a Web service on the computer running Microsoft Dynamics CRM and connect to it from your client-side JScript code. This is preferred because you can write the majority of your code within the Web Service in Microsoft Visual C# or Microsoft Visual Basic .NET using IntelliSense.

Note   A folder named 'ISV' has been provided in the Microsoft Dynamics CRM Web site and the local Web site for the Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access where you can place the code in the context of the application.

By creating your own Web service to contain the code used to access CRM, you can avoid the more challenging process of JScript string manipulation and identifying the correct structure for your SOAP envelope contents.

In Microsoft Dynamics CRM Online you cannot create a Web service application on the server so you must use XMLHttp requests to the Microsoft Dynamics CRM Web services. This can also be used to enable access to Microsoft Dynamics CRM Web services while the user is working offline with the Microsoft Dynamics CRM for Outlook.

GenerateAuthenticationHeader Function

The GenerateAuthenticationHeader function retrieves a properly formed Microsoft Dynamics CRM authentication header. This authentication header automatically determines the appropriate authentication type to use for each deployment type: on-premise, IFD, or Microsoft Dynamics CRM Online. Note that the SOAP header for IFD does not include information about the CrmTicket because a session-based ticketing system is used. For more information, see Authentication.

Each XMLHttp request requires a <soap:Header>.


You can generate this header for your code by using the following function:


Without this function you would need to define a SOAP header in your code like the one here for the on-premise version:

    <CrmAuthenticationToken xmlns="">
      <AuthenticationType xmlns="">
      <OrganizationName xmlns="">
      <CallerId xmlns="">

Using ISV.config with Web Services.

JScript code that is included for ISV.Config is included as the content of the JavaScript attribute within the button or menuitem elements. The existence of certain characters in these attributes makes them invalid.

You must encode the values in your script to use them in ISV.Config. This can be done using find/replace in a text editor. The characters listed in the following table must be encoded.

Character Encoded value
< &lt;
" &quot;
& &amp;


The encoded JScript code to call a Web service will look like this sample, which calls the WhoAmI message with the Execute method to retrieve the ID of the current user.

The code is included within a button element in the application toolbar.

/*Define the soapBody for the WhoAmI request.*/
var soapBody = &quot;&lt;soap:Body>&quot;+
 &quot;&lt;Execute xmlns=';+
 &quot;&lt;Request xsi:type='WhoAmIRequest' /> &quot;+
/*Wrap the Soap Body in a soap:Envelope.*/
 var soapXml = &quot;&lt;soap:Envelope &quot; +
 &quot;xmlns:soap='' &quot;+
 &quot;xmlns:xsi='' &quot;+
 soapXml += GenerateAuthenticationHeader();
 soapXml += soapBody;
 soapXml += &quot;&lt;/soap:Envelope>&quot;;

/* Create the XMLHTTP object for the execute method.*/
var xmlhttp = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);;POST&quot;, &quot;/MSCRMservices/2007/crmservice.&quot;+
 &quot;asmx&quot;, false);
 xmlhttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; &quot;+
 xmlhttp.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.&quot;+
/* Send the XMLHTTP object. */
/* Create an XML object to parse the results.*/
 xmlDoc= new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
/* Get the user's ID. */
var userid;
  var rawUserid = xmlDoc.getElementsByTagName(&quot;UserId&quot;)[0].childNodes[0].nodeValue;
  userid = &quot;{&quot;+rawUserid.toUpperCase()+&quot;}&quot;;
  alert(&quot;Your userid is: &quot;+userid);
 catch (e)
  err = xmlDoc.getElementsByTagName(&quot;description&quot;)[0].childNodes[0].nodeValue;  
  alert(&quot;Error :&quot;+e.description+&quot; : &quot;+err);
    <Title LCID ="1033" Text="WhoAmI"/>

See Also


Using Microsoft Dynamics URLs

© 2010 Microsoft Corporation. All rights reserved.