Using the CRM API SOAP Request from Script

You can use JavaScript to execute CRM Web service methods by constructing a SOAP message. By using this handy technique, you can perform rather interesting and complex logic in JavaScript. For example, the following code shows the full SOAP XML used to retrieve the fullname of an existing contact record.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <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">OrganizationName</OrganizationName>
      <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-
        0000-000000000000</CallerId>
    </CrmAuthenticationToken>
  </soap:Header>
  <soap:Body>
    <Retrieve xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
      <entityName>contact</entityName>
      <id>b07be4aa-f87b-dc11-8276-0003ff8a2b47</id>
      <columnSet xmlns:q1="http://schemas.microsoft.com/crm/2006/Query" xsi:type="q1:
        ColumnSet">
        <q1:Attributes>
          <q1:Attribute>fullname</q1:Attribute>
        </q1:Attributes>
      </columnSet>
    </Retrieve>
  </soap:Body>
</soap:Envelope>

Note

Note

In the previous script, you need to provide the correct OrganizationName and CallerId values. Microsoft Dynamics CRM provides a helper method named GenerateAuthenticationHeader that can be used to simplify construction of the CrmAuthenticationToken node. We discuss the GenerateAuthenticationHeader method later in the chapter.

You can call any Web service using this technique, including the Microsoft Dynamics CRM CrmService and MetadataService. The Microsoft Dynamics CRM SDK has examples of the SOAP XML for most of the common methods. However, you can always use a tool suchas Fiddler to capture the HTTP requests and determine the SOAP request you need to construct.

Note

Note

Both the Microsoft Dynamics CRM SDK and Working with Microsoft Dynamics CRM 4.0 (Microsoft Press, 2008) discuss in depth how to use Fiddler to capture Microsoft Dynamics CRM Web service requests.

Rather than building the CrmAuthenticationToken manually each time, Microsoft Dynamics CRM provides the GenerateAuthenticationHeader method to do this for you. Using the GenerateAuthenticationHeader method not only saves you lines of code but also provides the proper organization, so be sure to use it.

The following script function shows the SOAP required to query the MetadataService for information about a specific attribute.

function RetrieveAttributeInfomration(entity, attributeName)
{
  var serverUrl = "/MSCrmServices/2007/MetadataService.asmx";
  var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open("POST", serverUrl, false);
  xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
  xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/
    Execute")

  // Define the retrieve message
  var message =
  [
    "<?xml version='1.0' encoding='utf-8'?>",
    "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ",
    "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ",
    "xmlns:xsd="http://www.w3.org/2001/XMLSchema">",
    GenerateAuthenticationHeader(),
    "<soap:Body>",
    "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
    "<Request xsi:type='RetrieveAttributeRequest'>",
    "<MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>",
    "<EntityLogicalName>",
    entity,
    "</EntityLogicalName>",
    "<LogicalName>",
    attributeName,
    "</LogicalName>",
    "<RetrieveAsIfPublished>true</RetrieveAsIfPublished>",
    "</Request></Execute>",
    "</soap:Body>",
    "</soap:Envelope>"
  ].join("");

  xmlhttp.send(message);
  return xmlhttp.responseXML.xml;
}

Tip

Tip

Be sure that you have the correct URL for the the SOAPAction header.

By creating the SOAP call directly with script, you enjoy the following benefits:

  • You can use the same script with either a Microsoft Dynamics CRM Online or an on-premise deployment.

  • You can easily deploy the script logic by using Microsoft Dynamics CRM export/import customizations.

  • The script works in offline mode without additional deployment requirements.

These benefits can be useful in certain circumstances, but you should also consider the following drawbacks:

  • More effort is required for development and maintenance because you need to determine the SOAP envelope for each message in the SDK.

  • Because your code is not compiled, it is more fragile, and errors in development can manifest themselves more easily.

  • You lose some reusability. You need to copy common functionality between multiple entities.

  • You cannot protect any important business logic from being viewed by others.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset