This documentation is archived and is not being maintained.

Accessing Microsoft Dynamics CRM Web Services

Dynamics
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>.

Example

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

GenerateAuthenticationHeader();

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

<soap:Header>
    <CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
      <AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
        0
      </AuthenticationType>
      <OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
        AdventureWorksCycle
      </OrganizationName>
      <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
        00000000-0000-0000-0000-000000000000
      </CallerId>
    </CrmAuthenticationToken>
  </soap:Header>

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.

CharacterEncoded value
<&lt;
"&quot;
&&amp;

Example

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.

<Root>
 <ToolBar>
  <Button
     JavaScript="
/*Define the soapBody for the WhoAmI request.*/
var soapBody = &quot;&lt;soap:Body>&quot;+
 &quot;&lt;Execute xmlns='http://schemas.microsoft.com/crm/2007/&quot;+
 &quot;WebServices'>&quot;+
 &quot;&lt;Request xsi:type='WhoAmIRequest' /> &quot;+
 &quot;&lt;/Execute>&lt;/soap:Body>&quot;;
 
/*Wrap the Soap Body in a soap:Envelope.*/
 var soapXml = &quot;&lt;soap:Envelope &quot; +
 &quot;xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' &quot;+
 &quot;xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' &quot;+
 &quot;xmlns:xsd='http://www.w3.org/2001/XMLSchema'>&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;);
xmlhttp.open(&quot;POST&quot;, &quot;/MSCRMservices/2007/crmservice.&quot;+
 &quot;asmx&quot;, false);
 xmlhttp.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; &quot;+
 &quot;charset=utf-8&quot;);
 xmlhttp.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.&quot;+
 &quot;microsoft.com/crm/2007/WebServices/Execute&quot;);
 
/* Send the XMLHTTP object. */
  xmlhttp.send(soapXml);
  
/* Create an XML object to parse the results.*/
 xmlDoc= new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
 xmlDoc.async=false;
 xmlDoc.loadXML(xmlhttp.responseXML.xml);
 
/* Get the user's ID. */
var userid;
  try
 {
  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);
 }
">
   <Titles>
    <Title LCID ="1033" Text="WhoAmI"/>
   </Titles>
  </Button>
 </ToolBar>
</Root>

See Also

Concepts

Using Microsoft Dynamics URLs


© 2010 Microsoft Corporation. All rights reserved.


Show: