locked
fetch xml in crm 2011 online RRS feed

  • Question

  • Hi all,

     

    any way to get this code sample that is already working for crm 4 to work for crm 2011 RTW ??

     

     

    var authenticationHeader = window.opener.GenerateAuthenticationHeader();

    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\">" +

        authenticationHeader +

        "<soap:Body>" +

        "<RetrieveMultiple xmlns=" +

        "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

        "<query xmlns:q1=" +

        "\"http://schemas.microsoft.com/crm/2006/Query\" " +

        "xsi:type=\"q1:QueryByAttribute\">" +

        "<q1:EntityName>account</q1:EntityName>" +

        "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

        "<q1:Attributes>" +

        "<q1:Attribute>name</q1:Attribute>" +

        "</q1:Attributes>" +

        "</q1:ColumnSet>" +

        "<q1:Attributes>" +

        "<q1:Attribute>accountid</q1:Attribute>" +

        "</q1:Attributes>" +

        "<q1:Values>" +

        "<q1:Value xsi:type=\"xsd:string\">" + crmForm.ObjectId + "</q1:Value>" +

        "</q1:Values>" +

        "</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 resultXml = xmlHttpRequest.responseXML.xml;

     

    var doc = new ActiveXObject("MSXML2.DOMDocument");

    doc.async = false;

    doc.loadXML(resultXml);

    var name = doc.selectSingleNode("//q1:name");

    alert(name.text);

     

     

    thanks in advance

    Sunday, February 20, 2011 12:50 PM

Answers

  • Hi,

     

    I could make it work with the following code (as I used in crm 4). Basically I wanted to get a View FetchXml via REST service and than execute the Fetch with javascript to get the result list:

    function Fetch(xml) {

              var Xml = "<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\">"

              Xml += GenerateAuthenticationHeader()

              Xml += "<soap:Body>";

              Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">";

              Xml += "<fetchXml>";

              Xml += FetchEncode(xml); // Microsoft _HtmlEncode function  

              Xml += "</fetchXml>";

              Xml += "</Fetch>";

              Xml += "</soap:Body>";

              Xml += "</soap:Envelope>";

     

              var XmlHttp = CreateXmlHttpObject(); // Microsot CreateXmlHttp function  

              XmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false); //Sync Request  

              XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");

              XmlHttp.send(Xml);

     

              var XmlDoc = new ActiveXObject("Msxml2.DOMDocument");

              XmlDoc.async = false;

              XmlDoc.resolveExternals = false;                    

              XmlDoc.loadXML(XmlHttp.responseXML.text);

              return XmlDoc;

          }

     

          function CreateXmlHttpObject() //CreateXmlHttp  

          {

              var oXmlHttp = null;

              if (window.XMLHttpRequest) {

                  oXmlHttp = new XMLHttpRequest();

              }

              else {

                  var arrProgIds = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];

                  for (var iCount = 0; iCount < arrProgIds.length; iCount++) {

                      try {

                          oXmlHttp = new ActiveXObject(arrProgIds[iCount]);

                          break;

                      }

                      catch (e) { }

                  }

              }

     

              return oXmlHttp;

          }

     

          function FetchEncode(strInput) //_HtmlEncode  

          {

              var c;

              var HtmlEncode = '';

     

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

                      HtmlEncode = HtmlEncode + String.fromCharCode(c);

                  }

                  else {

                      HtmlEncode = HtmlEncode + '&#' + c + ';';

                  }

              }

              return HtmlEncode;

          }

     

     

     

     

     

    Then just invoke the Fetch function passing your FetchXml as an argument.

     

     

     

     

     

    Thursday, March 31, 2011 1:46 PM

All replies

  • Hi all,

     

    any way to get this code sample that is already working for crm 4 to work for crm 2011 RTW ??

     

    var authenticationHeader = window.opener.GenerateAuthenticationHeader();

    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\">" +

        authenticationHeader +

        "<soap:Body>" +

        "<RetrieveMultiple xmlns=" +

        "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

        "<query xmlns:q1=" +

        "\"http://schemas.microsoft.com/crm/2006/Query\" " +

        "xsi:type=\"q1:QueryByAttribute\">" +

        "<q1:EntityName>account</q1:EntityName>" +

        "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

        "<q1:Attributes>" +

        "<q1:Attribute>name</q1:Attribute>" +

        "</q1:Attributes>" +

        "</q1:ColumnSet>" +

        "<q1:Attributes>" +

        "<q1:Attribute>accountid</q1:Attribute>" +

        "</q1:Attributes>" +

        "<q1:Values>" +

        "<q1:Value xsi:type=\"xsd:string\">" + crmForm.ObjectId + "</q1:Value>" +

        "</q1:Values>" +

        "</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 resultXml = xmlHttpRequest.responseXML.xml;

     

    var doc = new ActiveXObject("MSXML2.DOMDocument");

    doc.async = false;

    doc.loadXML(resultXml);

    var name = doc.selectSingleNode("//q1:name");

    alert(name.text);

     

    thanks in advance

    Sunday, February 20, 2011 12:48 PM
  • Hello Mostafa,

    For fetchxml, please read: http://social.microsoft.com/Forums/en/crm2011beta/thread/9f0ad020-45c1-44ec-87c6-bc7d4dac385e

    And for SOAP, read: http://www.bizforward.cws-international.com/2011/01/26/creating-records-in-crm-2011-using-javascript/


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Sunday, February 20, 2011 2:32 PM
  • Dear Cornel,

     

    would you please provide me with a simple example that uses fetch xml with jscript in crm 2011 online

    Sunday, February 20, 2011 2:48 PM
  • Mostafa El Moatassem bellah is not the only one who needs help on this.

     

    I do need help it too.

     

    I have tried to follow CMR SDK 2011 REST examples, but I haven't manage to get it work, although I manage to get an entity via the URL, but not to get the results in Javascript from the URL :S  

    Monday, February 21, 2011 5:25 PM
  • any suggestions ???
    Tuesday, February 22, 2011 9:30 AM
  • any suggestions ???
    Tuesday, February 22, 2011 9:30 AM
  • If you search fetchXml in the sdk 2011 you will get alot of examples like:-
      
           ExecuteFetchRequest request = new ExecuteFetchRequest();
          request.FetchXml =
    @"
      <fetch distinct='false' mapping='logical' aggregate='true'>
        <entity name='account'>
          <attribute name='address1_latitude' aggregate='sum' alias='sum_address1_latitude'/>
        </entity>
      </fetch>"
    ;
    
          ExecuteFetchResponse response = (ExecuteFetchResponse)ExecuteRequest(request, typeof(ExecuteFetchResponse));
    
          Log.Comment("FetchXmlResult: {0}", response.FetchXmlResult);
    
          XmlDocument doc = new XmlDocument();
          doc.LoadXml(response.FetchXmlResult);
    
          double actual = Double.Parse(doc.DocumentElement.SelectSingleNode("//resultset/result/sum_address1_latitude").InnerText);
     
    

    Regards Faisal
    Tuesday, February 22, 2011 10:25 AM
  • Dear Faisal,

     

    I need to do it with javascript

    Tuesday, February 22, 2011 10:56 AM
  • try this:-
    
    <a href="http://crm2011scriptconvert.codeplex.com/">http://crm2011scriptconvert.codeplex.com/</a>
    
    <a href="http://bingsoft.wordpress.com/2010/09/09/crm-4-to-crm-2011-javascript-converter-tool/">http://bingsoft.wordpress.com/2010/09/09/crm-4-to-crm-2011-javascript-converter-tool/</a>
    

    http://social.microsoft.com/Forums/en/crm2011beta/thread/50082795-8277-455e-bf00-1385c0a98483


     

     

    Regards Faisal

    Tuesday, February 22, 2011 12:23 PM
  • Dear Faisal,

     

    I've used the mentioned tool and it provides me with the following results:

     

    var authenticationHeader = window.opener.Xrm.Page.context.getAuthenticationHeader();

     

    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\">" +

     

        authenticationHeader +

     

        "<soap:Body>" +

     

        "<RetrieveMultiple xmlns=" +

     

        "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

     

        "<query xmlns:q1=" +

     

        "\"http://schemas.microsoft.com/crm/2006/Query\" " +

     

        "xsi:type=\"q1:QueryByAttribute\">" +

     

        "<q1:EntityName>account</q1:EntityName>" +

     

        "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

     

        "<q1:Attributes>" +

     

        "<q1:Attribute>name</q1:Attribute>" +

     

        "</q1:Attributes>" +

     

        "</q1:ColumnSet>" +

     

        "<q1:Attributes>" +

     

        "<q1:Attribute>accountid</q1:Attribute>" +

     

        "</q1:Attributes>" +

     

        "<q1:Values>" +

     

        "<q1:Value xsi:type=\"xsd:string\">" + Xrm.Page.data.entity.getId() + "</q1:Value>" +

     

        "</q1:Values>" +

     

        "</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 resultXml = xmlHttpRequest.responseXML.xml;

     

     

     

    var doc = new ActiveXObject("MSXML2.DOMDocument");

     

    doc.async = false;

     

    doc.loadXML(resultXml);

     

    var name = doc.selectSingleNode("//q1:name");

     

    alert(name.text);

     

    but it didn't get to work

    Tuesday, February 22, 2011 1:56 PM
  • Moving over to development

    Regards, Donna

    Tuesday, March 1, 2011 12:54 AM
  • Same question here.
    Thursday, March 3, 2011 4:30 PM
  • Any solution found for this? Having same problem
    Thursday, March 3, 2011 4:37 PM
  • Thursday, March 3, 2011 10:58 PM
  •  the fetchXml overthere doesn't come in a Jscript format. Is there any solution?
    Monday, March 28, 2011 11:00 AM
  • Hi,

     

    I could make it work with the following code (as I used in crm 4). Basically I wanted to get a View FetchXml via REST service and than execute the Fetch with javascript to get the result list:

    function Fetch(xml) {

              var Xml = "<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\">"

              Xml += GenerateAuthenticationHeader()

              Xml += "<soap:Body>";

              Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">";

              Xml += "<fetchXml>";

              Xml += FetchEncode(xml); // Microsoft _HtmlEncode function  

              Xml += "</fetchXml>";

              Xml += "</Fetch>";

              Xml += "</soap:Body>";

              Xml += "</soap:Envelope>";

     

              var XmlHttp = CreateXmlHttpObject(); // Microsot CreateXmlHttp function  

              XmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false); //Sync Request  

              XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

              XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");

              XmlHttp.send(Xml);

     

              var XmlDoc = new ActiveXObject("Msxml2.DOMDocument");

              XmlDoc.async = false;

              XmlDoc.resolveExternals = false;                    

              XmlDoc.loadXML(XmlHttp.responseXML.text);

              return XmlDoc;

          }

     

          function CreateXmlHttpObject() //CreateXmlHttp  

          {

              var oXmlHttp = null;

              if (window.XMLHttpRequest) {

                  oXmlHttp = new XMLHttpRequest();

              }

              else {

                  var arrProgIds = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];

                  for (var iCount = 0; iCount < arrProgIds.length; iCount++) {

                      try {

                          oXmlHttp = new ActiveXObject(arrProgIds[iCount]);

                          break;

                      }

                      catch (e) { }

                  }

              }

     

              return oXmlHttp;

          }

     

          function FetchEncode(strInput) //_HtmlEncode  

          {

              var c;

              var HtmlEncode = '';

     

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

                      HtmlEncode = HtmlEncode + String.fromCharCode(c);

                  }

                  else {

                      HtmlEncode = HtmlEncode + '&#' + c + ';';

                  }

              }

              return HtmlEncode;

          }

     

     

     

     

     

    Then just invoke the Fetch function passing your FetchXml as an argument.

     

     

     

     

     

    Thursday, March 31, 2011 1:46 PM
  • Hi all,

    Here its the following code i used for fetch xml, Im getting error saying "Invalid Argument" in CRM 2011. No Idea from where im getting that Error.I called Fetch in the Form Properties on Onload Event

     function Fetch()
    {

            var EmailTo = Xrm.Page.getAttribute("to").getValue();
        
        var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical">';
        // Target Entity Name
        fetchXml += '<entity name="contact">';
        // Required Attribute
        fetchXml += '<attribute name="emailaddress1"/>';
        // Condition
        fetchXml += '<filter type="and">';
        fetchXml += '<condition attribute="lastname" operator="eq" value="'+EmailTo+'" />';
        fetchXml += '</filter>';
        fetchXml += '</entity>';
        fetchXml += '</fetch>';
        
        var Xml = "<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\">"
        Xml += GenerateAuthenticationHeader()
        Xml += "<soap:Body>";
        Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">";
        Xml += "<fetchXml>";
        Xml += _HtmlEncode(fetchXml);
        Xml += "</fetchXml>";
        Xml += "</Fetch>";
        Xml += "</soap:Body>";
        Xml += "</soap:Envelope>";
        
        var XmlHttp = CreateXmlHttp();
        
        XmlHttp.open("POST", 'http://<Server Name>:Port/mscrmservices/2007/crmservice.asmx', false );
        XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
        XmlHttp.send(Xml);
        var resultDoc = loadXmlDocument(XmlHttp.responseXML.text);
        var resultnames = resultDoc.selectNodes("//emailaddress1");
        if( resultRecords.length == 1 )
        {
          alert('Product ' +resultnames.text );
        }
    }
             function _HtmlEncode(strInput)
    {
          //_HtmlEncode
          var c;
          var HtmlEncode = '';
          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 ))
                                   {
            HtmlEncode = HtmlEncode + String.fromCharCode(c);
           }
           else{
            HtmlEncode = HtmlEncode + '&#' + c + ';';
           }
          }
          return HtmlEncode;
         }
                          
             function CreateXmlHttp()
    {
          //CreateXmlHttp
          var oXmlHttp = null;
          if(window.XMLHttpRequest){
           oXmlHttp = new XMLHttpRequest();
          }
          else{
           var arrProgIds = ["Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
           for(var iCount = 0; iCount < arrProgIds.length;iCount++){
            try{
             oXmlHttp = new ActiveXObject(arrProgIds[iCount]);
             break;
            }
            catch(e){}
           }
          }
          return oXmlHttp;
         }

    Any suggestion.

    Tuesday, March 6, 2012 2:53 AM