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

27 thoughts on “Calling External Webservice from MS CRM 2011 Asynchronously – Javascript

    1. mahenderpal Post author

      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
    1. mahenderpal Post author

      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
  1. Shweta

    Hi Mahender,

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

    Thanks in advance

    Reply
    1. mahenderpal Post author

      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
      1. 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

        Reply
  2. 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

    Reply
    1. mahenderpal Post author

      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
  3. 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.

    Reply
  4. 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

    Reply
  5. 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.

    Reply
  6. 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.

    Reply
    1. mahenderpal Post author

      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.

      Reply
  7. James

    Mahender, do you have a sample that shows this function being called from within CRM? I’m new to working with web services and am not sure what goes in the ProxyURL, etc. I want to create a function I can re-use and need to know how to make it more generic. For example, I see tempuri.org included within the code, as opposed to being passed into the function. Thanks!

    Reply
  8. 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

    Reply
    1. mahenderpal Post author

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

      Reply
      1. 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

        Reply
    1. mahenderpal Post author

      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.

      Reply
      1. 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!

        Reply
  9. 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!

    Reply
  10. prashanth M

    I am also getting permission denied error after replacing xmlhttp with xmlHttp.
    However i am not sure whether i am passing correct inputs to functions
    i am using following parameters let me know if i am doing something wrong , I am passing city,county as parameter and getting whether for these city

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *