Tuesday, 28 April 2015

Query FetchXml thorugh Soap Request

FetchXml can be query using Soap call. There are plenty of libraries available that you can use to make this task work but i will explain you how to make it own library.

There are three steps to that, which are as follows:
1) Build your XML and Encode them in XML Document object, for example

 var fetchQuery="<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"><entity name=\"lead\"><attribute name=\"fullname\" /><attribute name=\"companyname\" /><attribute name=\"telephone1\" /><attribute name=\"leadid\" /><order attribute=\"fullname\" descending=\"false\" /><link-entity name=\"account\" from=\"accountid\" to=\"parentaccountid\" alias=\"ad\"><filter type=\"and\"><condition attribute=\"accountid\" operator=\"eq\" uiname=\"A. Datum Corporation (sample)\" uitype=\"account\" value=\"{A9D32F2F-58D4-E411-
80F0-C4346BAC094C}\" /></filter></link-entity></entity></fetch>";

    var encodedFetchQuery = _xmlEncode(fetchQuery);
 
 function _xmlEncode(strInput) {
  var c;
  var XmlEncode = '';
  if (strInput == null) {
   return null;
  }
  if (strInput == '') {
   return '';
  }
  for (var cnt = 0; cnt < strInput.length; cnt++) {
   c = strInput.charCodeAt(cnt);
   if (((c > 96) && (c < 123)) ||
            ((c > 64) && (c < 91)) ||
            (c == 32) ||
            ((c > 47) && (c < 58)) ||
            (c == 46) ||
            (c == 44) ||
            (c == 45) ||
            (c == 95)) {
    XmlEncode = XmlEncode + String.fromCharCode(c);
   }
   else {
    XmlEncode = XmlEncode + '&#' + c + ';';
   }
  }
  return XmlEncode;
 }
 
2) Make a Soap Envelop, for example
 
var request = [
        '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><
s:Body>',
            '<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" >',
                '<request i:type="a:RetrieveMultipleRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">',
                    '<a:Parameters xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">',
                        '<a:KeyValuePairOfstringanyType>',
                            '<b:key>Query</b:key>',
                            '<b:value i:type="a:FetchExpression">',
                                '<a:Query>', encodedFetchQuery , '</a:Query>',
                            '</b:value>',
                        '</a:KeyValuePairOfstringanyType>',
                    '</a:Parameters>',
                    '<a:RequestId i:nil="true"/>',
                    '<a:RequestName>RetrieveMultiple</a:RequestName>',
                '</request>',
            '</Execute>',
        '</s:Body></s:Envelope>'
    ].join("");
 
3) Make a soap Request, for example
 
//Synchronous XMLHttpRequest to retrieve contact records
    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(Xrm.Page.context.
getClientUrl() + "/XRMServices/2011/Organization.svc/web"), false);
    try { req.responseType = 'msxml-document' } catch (e) { }
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction",     "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");  
      req.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            req.onreadystatechange = null; //Addresses potential memory leak issue with IE
            if (this.status == 200  /*success*/) {
                var doc = req.responseXML;
                successCallback(doc);
            }
            else {
                errorCallback(accountId);
            }
        }
    }
    req.send(request);
 
You will get your output in  req.responseXML.xml in XML format. its your choice how you wish to use it.

Thanks.
 
 

No comments:

Post a Comment