Friday, 17 March 2017

RetrieveMultiple method using Javascript

This sample shows how to use the CrmService.RetrieveMultiple method using the same example provided in the following topic: CrmService.RetrieveMultiple method.


// Prepare variables to retrieve the contacts.
var searchCity = "Sammamish";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = ""+ 
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
authenticationHeader+ 
""+ 
""+ 
"" xsi:type='q1:QueryExpression'>"+ 
"contact"+ 
""+ 
""+ 
"fullname"+ 
"contactid"+ 
""+ 
""+ 
"false"+ 
""+ 
"And"+ 
""+ 
""+ 
"address1_city"+ 
"Like"+ 
""+ 
""+searchCity+""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
"";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
 var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
 alert(msg);
}
// Parse and display the results.
else
{
 var results = resultXml.getElementsByTagName('BusinessEntity');
 var msg = "";
 if (results.length == 0)
 {
  msg = "No contacts were found in "+searchCity+".";
  alert(msg);
  return;
 }
 else
 {
  msg = "Contacts found in "+searchCity+":\r";
  msg += "\Contact Id\t\t\t\tContact Full Name\r";
  msg +="--------------------------------------------------------------------------------\r";
    for (i=0;i < results.length;i++)
    {
     var idValue = results[i].selectSingleNode('./q1:contactid').nodeTypedValue;
     var name = results[i].selectSingleNode('./q1:fullname').nodeTypedValue;
     msg += idValue +"\t"+ name+"\r";
    }
    alert(msg);
 }
}

No comments :

Post a Comment