Calling External Webservice from MS CRM 2011 Asynchronously – Javascript


If you are looking for the code to call external webservice from MS CRM 2011, you can create a webresource in MS CRM 2011 and call use below code

function CallCustomWebService(FunctionName, ParameterNameList, ParameterValueList,ProxyURL)

{//FunctionName-Nameof the function to call

//ParameterNameList-Array of Parameter names in your proxy function

//ParameterValueList-Array of Parameter value

//ProxyURL-URL of your proxy

var CallingFunctionURL = “http://tempuri.org/” +FunctionName;

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’>”+

“<soap:Body>” +

“<” + FunctionName + ” xmlns=\’http://tempuri.org/\’>”;

for (i = 0; i< ParameterNameList.length; i++) {

xml = xml + “<” +ParameterNameList[i] + “>” + ParameterValueList[i] + “</” +ParameterNameList[i] + “>”;

}

xml = xml + “</” +FunctionName + “>”;

xml = xml +“</soap:Body></soap:Envelope>”;

xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);

xmlHttp.open(“POST”,ProxyURL, true);

xmlHttp.setRequestHeader(“Content-Type”,“text/xml; charset=utf-8″);

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

xmlHttp.setRequestHeader(“SOAPAction”,CallingFunctionURL);

xmlHttp.onreadystatechange =function() {

ParseResult(xmlhttp);

};

xmlHttp.send(xml);

}

 

function ParsetResult(XmlRequest)

{

if(XmlRequest.readyState == 4) {

if(XmlRequest.status == 200) {
alert(XmlRequest.responseXML.text);

//Fetch your result from response xml here

}

 

else {

alert(“Request Failed ”);

}

 

}

}

Enjoy !!!

 

 

10 Comments (+add yours?)

  1. Sjøl
    Dec 02, 2011 @ 19:48:48

    And where do you place this code??

    Reply

    • mahenderpal
      Dec 06, 2011 @ 08:25:25

      you can place this code in webresource and call your function based on your requirement either on onsave of form or onchange of control.

      Reply

  2. DC 7
    Apr 26, 2012 @ 08:40:14

    Can you provide the code in the ParseResult function that fetch the result from response xml?

    Reply

    • mahenderpal
      Apr 26, 2012 @ 08:51:50

      you can use below code to create xmldocument from result, then check _ResultResponse object for values
      var oXmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
      oXmlDoc.async = false;
      // Load the XML document that has the UnEncoded results.
      oXmlDoc.loadXML(XmlRequest.responseXML.text);
      var _ResultResponse = oXmlDoc.getElementsByTagName(‘Result’);

      Reply

  3. DC 7
    Apr 29, 2012 @ 01:34:29

    Thanks Mahender. I will try that out.

    Reply

  4. Shweta
    May 22, 2012 @ 11:50:01

    Hi Mahender,

    What value do I provide for proxy URL parameter, in order to make this function work?

    Thanks in advance

    Reply

    • mahenderpal
      May 22, 2012 @ 14:19:45

      Hi Shweta,you need to provide your proxyservice URL and if your proxy function need some parameter then you need to pass parameter list and their values.

      let me know if you need any other information.

      Reply

  5. Shweta
    May 23, 2012 @ 07:24:41

    Hi Mahender,

    Thanks, I got around with this bit, but am now getting a 401 unauthorized error when trying to access my proxy web service mehod. Any idea as to what I might be missing?

    Thanks

    Reply

    • mahenderpal
      May 23, 2012 @ 08:53:56

      Are you able to access your webservice through browser ??, it seems your webservice needs authentication to connect, in that case you need to provide user credentials for this.

      Reply

  6. Shweta
    May 23, 2012 @ 10:01:21

    Hi,

    I am able to access my web service through browser, did some minor security tweaks. Now its throwing a 400 Bad Request error, something to do with the request that is getting formulated.

    Reply

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers