Mohammed Atef’s Technical blog

Call Microsoft Dynamics CRM Web Services

Introduction
did you ever tried to call MS CRM Dynamics web service before? It is very simple but you must be familiar with JavaScript.  

Generate the CRM Header

It is possible to access the Microsoft Dynamics CRM Web services from your client-side JScript code.

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.

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.

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

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>

Call CRM web service

After generating the header you can use it as shown in the below code

//Prepare variables for a contact to retrieve.

var contactid = “4696f8cb-9a1c-dd11-ad3a-0003ff9ee217”;

var authenticationHeader = GenerateAuthenticationHeader();

 

//Prepare the SOAP message.

var xml = “<?xml version=’1.0′ encoding=’utf-8′?>”+

“<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/'&#8221;+

” xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'&#8221;+

” xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>&#8221;+

authenticationHeader+

“<soap:Body>”+

“<Retrieve xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>&#8221;+

“<entityName>contact</entityName>”+

“<id>”+contactid+“</id>”+

“<columnSet xmlns:q1=’http://schemas.microsoft.com/crm/2006/Query&#8217; xsi:type=’q1:ColumnSet’>”+

“<q1:Attributes>”+

“<q1:Attribute>fullname</q1:Attribute>”+

“</q1:Attributes>”+

“</columnSet>”+

“</Retrieve>”+

“</soap:Body>”+

“</soap:Envelope>”;

//Prepare the xmlHttpObject and send the request.

var xHReq = new ActiveXObject(“Msxml2.XMLHTTP”);

xHReq.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

xHReq.setRequestHeader(“SOAPAction”,http://schemas.microsoft.com/crm/2007/WebServices/Retrieve&#8221;);

xHReq.setRequestHeader(“Content-Type”, “text/xml; charset=utf8”);

xHReq.setRequestHeader(“Content-Length”, xml.length);

xHReq.send(xml);

//Capture the result.

var resultXml = xHReq.responseXML;

//Check for errors.

var errorCount = resultXml.selectNodes(‘//error’).length;

if (errorCount != 0)

{

     var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;

     alert(msg);

}

//Display the retrieved value.

else

{alert(resultXml.selectSingleNode(“//q1:fullname”).nodeTypedValue);}

I hope this help.

Advertisements

April 30, 2009 - Posted by | CRM Dynamics 4.0 | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: