Call crm webservice through Javascript


Most of the time developer need to call crm webservice or external webservice through javascript, here is the code to call crm webservice through javascript.

 var sEntityName=”nameofentity”;
 var xml = “<?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>” +
            ” <RetrieveMultiple xmlns=\”http://schemas.microsoft.com/crm/2007/WebServices\“>” +
            ” <query xmlns:q1=\”http://schemas.microsoft.com/crm/2006/Query\” xsi:type=\”q1:QueryExpression\”>” +
            ” <q1:EntityName>” + sEntityName + “</q1:EntityName>” +
            ” <q1:ColumnSet xsi:type=\”q1:ColumnSet\”>” +
                                               //List of attributes
       ” <q1:Attributes>” +
                                                            ” <q1:Attribute>FirstAttribute</q1:Attribute>” +//In the same way you can add more attributes
                                                ” </q1:Attributes>” +
                                    ” </q1:ColumnSet>” +
                                    ” <q1:Distinct>false</q1:Distinct>” +      
                                    ” <q1:Criteria>” +
                                    ” <q1:FilterOperator>And</q1:FilterOperator>” +

     //Add conditions to your query
      ” <q1:Conditions>” +
                                                ” <q1:Condition>” +
                                                ” <q1:AttributeName>NameofAttribute</q1:AttributeName>” +
                                                ” <q1:Operator>Equal</q1:Operator>” +
                                                ” <q1:Values>” +
                                                            ” <q1:Value>”+Guid+”</q1:Value>” +
                                                ” </q1:Values>” +
                                                ” </q1:Condition>” +
                                                ” </q1:Condition>” +       
                                            ” </q1:Conditions>” +
                                    ” </q1:Criteria>” +                     
                        ” </query>” +
            ” </RetrieveMultiple>” +
            ” </soap:Body>” +
            “</soap:Envelope>”;

            //Use XMLHTTP to send request to MSCRM using the webservice CrmService.asmx
            var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
            xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);
 
            xmlHttpRequest.setRequestHeader(“SOAPAction”,” http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple“);
            xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
            xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);
            xmlHttpRequest.send(xml);
            //Getting the Response and Parsing
            var resultXml = xmlHttpRequest.responseXML;
            //select the node text
             alert(resultXml.selectSingleNode(“//q1:FirstAttributeName”).nodeTypedValue);
            

Hope it will help somebody.

Advertisements

4 Comments

Filed under MS CRM & Javascript

4 responses to “Call crm webservice through Javascript

  1. Manu

    Does this procedure
    apply to Dynamics Crm Online or just for CRM 4.0?

    • mahenderpal

      this is for MS CRM 4.0, you can use it in MS CRM 2011 also but it will stop working after some time in CRM 2011 because I have used 2007 endpoints here.

  2. Manu

    Can you pls help me out?
    Im using Dynamics Crm Online and trying to call the Crm Webservice using javascript.Im unable to do it with GenerateAuthenticationHeader().
    So i created a Soap Header.But unable to connect and getting this Error: Access Denied.
    Pls help….

  3. Manu

    I generated a Soap Envelope and Soap body using my Server(url) WSDL in SOAP UI and tried to get the response.But it show java.socket error . Then i used that Request for the “RETRIEVE” Method in my Javascript passing some parameters like Eg:Quote ID ,which is a CRM generated field to retrieve other fields relating to that form.But im getting Access Denied error…

    Pls Help….

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