locked
Calling custom webservice through javascript in CRM RRS feed

  • Question

  • i need to send some xml data to my webservice like

    public string GetResponse(string strXmlRequest)
        {

    XmlDocument doc=new XmlDocument();
    doc.load(strXmlRequest);
       }

            and i amd sending data like.......
    <soap:Body>"+
    "<GetResponse xmlns=\'http://tempuri.org/\'>"+
    "<strXmlRequest>"+  <LossInfo>
    <LCategory>ABC</LCategory>
    <LCategoryId>110</LCategoryId>
    </LossInfo>
    +"</strXmlRequest>"+
    "</GetResponse>"+
    " </soap:Body>"+
    "</soap:Envelope>";


    but i am not getting any Http response if i send data like this .........Pls help me

    please let me knw if i am using wrong way to  send parameter.........


    thanks in advance...........


    mahender
    Thursday, March 19, 2009 1:41 PM

Answers

  • You need to set the proper header properties before calling the web services.  You will need to dig into the stock javascript to see how they set the headers.
    Matt, MVP - Dynamics CRM
    • Marked as answer by Jim Glass Jr Monday, March 23, 2009 7:55 PM
    Friday, March 20, 2009 9:54 PM
  • Hi, Have a look at the following (it contains the headers, you need GenerateAuthenticationHeader() )

    function(action, request) {  
     
        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
     
        xmlhttp.open("POST""/mscrmservices/2007/CrmService.asmx"false);  
     
        xmlhttp.setRequestHeader("Content-Type""text/xml; charset=utf-8");  
     
        xmlhttp.setRequestHeader("SOAPAction""http://schemas.microsoft.com/crm/2007/WebServices/" + action);  
     
        var soapMessage = "<?xml version='1.0' encoding='utf-8'?>" +  
     
            "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +  
     
                "xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +  
     
            GenerateAuthenticationHeader() +  
     
            "<soap:Body>" + request + "</soap:Body>" +  
     
            "</soap:Envelope>";  
     
        xmlhttp.send(soapMessage);  
     
        return xmlhttp.responseXML;  
     

    If you need more help, have a look at this: http://blog.customereffective.com/blog/2009/03/client-side-web-service-calls-for-microsoft-dynamics-crm-40-calling-the-crm-service-with-javascript.html


    sys admin
    • Marked as answer by Jim Glass Jr Monday, March 23, 2009 7:55 PM
    Saturday, March 21, 2009 3:27 AM

All replies

  • You need to set the proper header properties before calling the web services.  You will need to dig into the stock javascript to see how they set the headers.
    Matt, MVP - Dynamics CRM
    • Marked as answer by Jim Glass Jr Monday, March 23, 2009 7:55 PM
    Friday, March 20, 2009 9:54 PM
  • Hi, Have a look at the following (it contains the headers, you need GenerateAuthenticationHeader() )

    function(action, request) {  
     
        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
     
        xmlhttp.open("POST""/mscrmservices/2007/CrmService.asmx"false);  
     
        xmlhttp.setRequestHeader("Content-Type""text/xml; charset=utf-8");  
     
        xmlhttp.setRequestHeader("SOAPAction""http://schemas.microsoft.com/crm/2007/WebServices/" + action);  
     
        var soapMessage = "<?xml version='1.0' encoding='utf-8'?>" +  
     
            "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +  
     
                "xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +  
     
            GenerateAuthenticationHeader() +  
     
            "<soap:Body>" + request + "</soap:Body>" +  
     
            "</soap:Envelope>";  
     
        xmlhttp.send(soapMessage);  
     
        return xmlhttp.responseXML;  
     

    If you need more help, have a look at this: http://blog.customereffective.com/blog/2009/03/client-side-web-service-calls-for-microsoft-dynamics-crm-40-calling-the-crm-service-with-javascript.html


    sys admin
    • Marked as answer by Jim Glass Jr Monday, March 23, 2009 7:55 PM
    Saturday, March 21, 2009 3:27 AM
  • thanks everybody for response....

     but in my case i m not calling crm webservice i am calling my custom webservice. here is the code i have used to create header 
    soapRequest="<?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>"+
            "<"+StrFunctionName+" xmlns=\'http://tempuri.org/\'>"+
            "<strXmlRequest><Student><name>abc</name><roll>1</roll></student></strXmlRequest>"+
            "</"+StrFunctionName+">"+
            " </soap:Body>"+
            "</soap:Envelope>";
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            xmlHttp.open("post","http://localhost/myservice/Service.asmx", false);
            xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
            xmlHttp.setRequestHeader("Content-Length", soapRequest.length);
            xmlHttp.setRequestHeader("SOAPAction",StrFunctionURL);
            xmlHttp.send(soapRequest);


    but i m not getting any response.................

    where strXmlRequest is the string parameter in my webmethod.



    mahender
    Thursday, March 26, 2009 12:17 PM
  • A simple solution would be to call the WebService from ASP.NET or a utility like Storm and use Fiddler to see the soap message being passed. You can then use that in your Javascript.


    HTH,
    Maruf
    • Proposed as answer by Maruf Friday, May 29, 2009 4:02 PM
    Friday, May 29, 2009 4:02 PM