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 ParseResult(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 !!!

Advertisements

25 Comments

Filed under MS CRM 2011

25 responses to “Calling External Webservice from MS CRM 2011 Asynchronously – Javascript

  1. Sjøl

    And where do you place this code??

    • mahenderpal

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

  2. DC 7

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

    • mahenderpal

      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’);

  3. DC 7

    Thanks Mahender. I will try that out.

  4. Shweta

    Hi Mahender,

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

    Thanks in advance

    • mahenderpal

      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.

      • XFactor

        Hi,
        when you answered shweta with regards to the proxy url and passing parameters, what did you mean. please send an example for that.

        Thanks

  5. Shweta

    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

    • mahenderpal

      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.

  6. Shweta

    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.

  7. Karan

    Hi Mahendar

    Thanks a lot for this code. Its a great help.

    I am using this code, but getting an error. After debugging, I got the following message in the crmscripterrorreport :
    The value of the property ‘ParseResult’ is null or undefined, not a Function object

    Do you have any idea on this??

    Regards

  8. Karan

    Hi

    Got the problem. There is typo error in method name in the definition.

    Thanks.

  9. Karan

    Hi Mahendar

    I have a query. In the external web service function, there are 3 “out” parameters also.
    Do you have any idea how to implement the “out” parameters in the javascript for a soap call to the external service function.

    Any help will be highly appreciated.

  10. DFM

    This page looks like it would be very helpful if I knew more about the product. Unfortunately I’ve just recently become aware of Dynamics CRM 2011 and our company is pinning quite a lot of hope on its power. As a very new user could anyone direct me to resources in the way of books or training that would focus on accessing external data from within CRM? One example of what I would like to accomplish is setting up a dialog that allows the user to start by entering a site number when an item is booked. I’d like to query our business application via a web service and bring in the name, address, etc. if the site already exists.
    Thanks for any input.

    • mahenderpal

      Hi,
      you can integrate your custom applications with ms crm 2011 and get and pass data to your custom applicatoin. In dialogs you can query data which is available in MS CRM 2011.

  11. XFactor

    hi Mahenderpal,

    i am having problems with this code. i am getting a “request failed” error. when debugging, i get Object doesn’t support this property or method. what could be the problem?
    a Step by Step response would be highly appreciated.

    Thanks

    • mahenderpal

      In which line you are getting this error, please make sure you have changed quotes properly.

      • XFactor

        hi,

        the calling Url returns: “http://localhost:82/Service1.asmxundefined” has undefined instead of placing the function there.
        Please let me know what the proxy url should look like as well.
        the minute I move to line after the xml = xml + “”; then I get that error.

        Regards
        XFactor

  12. Mitch

    Hi I am new in using crm. Can i ask, how can i pass all my parameters into ParameterNameList array?

    Thanks!

    • mahenderpal

      Not sure if I understand your question, but you can just declare an array with all of your parameters and pass it to this function.

      • Mitch

        Thank you! I have another issue, I got “Error: Permission denied” calling the method. I tried enabling “Access data sources across domains” from Internet Options->Security->Custom Level->security setting->miscellaneous section and the error is gone. But, I encountered an error ‘xmlhttp’ is undefined.
        Any idea how to fix this? Thank you so much!

      • Rodrigo Correia

        Mitch,

        You have to replace xmlhttp with xmlHttp.

  13. dharam

    make the H capital to xmlhttp

  14. Mitch

    Thank you Rodrigo and dharam. I finally escaped the error about that.
    But I am getting the “Request Failed”. I think it is because of the proxy URL i provided. What value do I provide for proxy URL parameter? Is it my web service api url?

    Thank you so much!

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