Cross-Browser Solution For ActiveXObject with SOAP Retrieve Javascript CRM 2011 Update Rollup 2012

 We face an issue after update rollup that  Soap Request Through Javascript not working ActiveXobject is Not supported cross browser.We solve this issue using XMLHTTPREQUEST 
Following function works cross browser

function SoapRetrieve() {
   
    var accountid = Xrm.Page.getAttribute("account").getValue();
    if (accountid != null && accountid != "") {
        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\">" +
        Xrm.Page.context.getAuthenticationHeader() +
        "<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>account</q1:EntityName>" +
            "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
                "<q1:Attributes>" +
                    "<q1:Attribute>name</q1:Attribute>" +
                "</q1:Attributes>" +
            "</q1:ColumnSet>" +
        "<q1:Distinct>false</q1:Distinct>" +
        "<q1:PageInfo>" +
        "   <q1:PageNumber>1</q1:PageNumber>" +
        "   <q1:Count>1</q1:Count>" +
        "</q1:PageInfo>" +

        "<q1:Criteria>" +
        "<q1:FilterOperator>And</q1:FilterOperator>" +

        "<q1:Conditions>" +
                         "<q1:Condition>" +
                         "<q1:AttributeName>accountid</q1:AttributeName>" +
        "<q1:Operator>Equal</q1:Operator>" +
        "<q1:Values>" +
        "<q1:Value xmlns:q2='http://microsoft.com/wsdl/types/' xsi:type='q2:guid'>" +
        accountid[0].id +
        "</q1:Value></q1:Values></q1:Condition>" +

        "</q1:Conditions>" +
        "</q1:Criteria>" +

        "</query></RetrieveMultiple>" +
        "</soap:Body></soap:Envelope>";


        var xmlHttpRequest;
        var doc;
        var result;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlHttpRequest = new XMLHttpRequest();
            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);
            result = xmlHttpRequest.responseXML.xml;
           
        }
        else {// code for IE6, IE5
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

            xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
            xmlHttpRequest.send(xml);
            result = xmlHttpRequest.responseXML.xml;


        }
        if (window.DOMParser) {
            parser = new DOMParser();
            doc = parser.parseFromString(xmlHttpRequest.responseText, "text/xml");
        }
        else // Internet Explorer
        {
            doc = new ActiveXObject("MSXML2.DOMDocument");
            doc.async = false;
            doc.loadXML(result);
        }
        if (doc.getElementsByTagName("q1:name").length > 0) {
           var A = doc.getElementsByTagName("q1:name").item(0).textContent
            alert(A);
        }
        else {
           
            var A = doc.getElementsByTagName("name").item(0).textContent
            alert(A);
        }
    }
}

Comments

  1. This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
    programming, it consulting, retail, manufacturing, CRM, digital supply chain management, Delivering high-quality service for your business applications,
    Solutions for all Industries,
    Getting your applications talking is the key to better business processes,
    Rapid web services solutions for real business problems,
    Web-based Corporate Document Management System,
    Outsourcing Solution,
    Financial and Operations Business Intelligence Solution,

    prologic-corp

    ReplyDelete
  2. This blog is very much helpful to us. Thanks for your information

    Guest posting sites
    Education

    ReplyDelete

Post a Comment

Popular Posts