Thursday, 23 May 2013

using Soap in Java Script

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\">" + 
    GenerateAuthenticationHeader() +
    "<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>new_vehicle</q1:EntityName>" + 
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>" + 
    "<q1:Attributes>" + 
    "<q1:Attribute>new_vehicleid</q1:Attribute>" +
    "<q1:Attribute>new_primarydriver</q1:Attribute>" +
    "<q1:Attribute>statuscode</q1:Attribute>" +
    "<q1:Attribute>new_registration</q1:Attribute>" +
    "</q1:Attributes>" + 
    "</q1:ColumnSet>" + 
    "<q1:Distinct>false</q1:Distinct>" + 

    "<q1:Conditions>" + 

                     "<q1:Condition>" +
                     "<q1:AttributeName>new_primarydriver</q1:AttributeName>" +
    "<q1:Operator>Equal</q1:Operator>" +
    "<q1:Values>" +
    "<q1:Value xmlns:q2='http://microsoft.com/wsdl/types/' xsi:type='q2:guid'>" +
    customerID +
    "</q1:Value></q1:Values></q1:Condition>" + 

    "</q1:Conditions>" +

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


    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

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

    var result = xmlHttpRequest.responseXML.xml;
    var doc = new ActiveXObject("MSXML2.DOMDocument");
    doc.async = false;
    doc.loadXML(result);

    var id = doc.selectSingleNode("//new_vehicleid");
    var registration = doc.selectSingleNode("//new_registration");

    if(id == null)
       return null;

    var vehicle = new Array();
                     value[0] = new Object();
                     value[0].id = id;
                     value[0].name = registration;
                     value[0].entityType = "new_vehicle";