Mohammed Atef’s Technical blog

Call Microsoft Dynamics CRM Web Services

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


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=>





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=’'&#8221;+

” xmlns:xsi=’'&#8221;+

” xmlns:xsd=’’>&#8221;+



“<Retrieve xmlns=’’>&#8221;+



“<columnSet xmlns:q1=’; xsi:type=’q1:ColumnSet’>”+








//Prepare the xmlHttpObject and send the request.

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

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


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

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


//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;



//Display the retrieved value.



I hope this help.


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: