locked
FetchXml and Javascript - cannot get it to work RRS feed

  • Question

  • I have used the advanced find to build a fetch xml statement - so I know it is correct and does return a record.  However, when I try to impliment it in javascript I just cannot get it to work.  Here's my javascript code:

    PS. I have tried SOAPActions Fetch, Retrieve, and FetchXml.

     

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

     

    " <fetchXml xmlns='http://schemas.microsoft.com/crm/2007/WebServices\'>" +

     

    " <fetch mapping='logical' count='250' version='1.0'>" +

     

    " <entity name='new_phase'>" +

     

    " <attribute name='new_phaseid'/>" +

     

    " <attribute name='new_name'/>" +

     

    " <filter type='and'>" +

     

    " <condition attribute='new_teamid' operator='eq' value='" + teamId + "'/>" +

     

    " </filter>" +

     

    " <link-entity name='new_stageheader' from='new_stageheaderid' to='new_stageheaderid' alias='aa'>" +

     

    " <link-entity name='new_stagedetail' from='new_stageheaderid' to='new_stageheaderid' alias='ab'>" +

     

    " <filter type='and'>" +

     

    " <condition attribute='new_projectscheduleid' operator='eq' value='" + projectScheduleId + "'/>" +

     

    " </filter>" +

     

    " </link-entity>" +

     

    " </link-entity>" +

     

    " </entity>" +

     

    " </fetch>" +

     

    " </fetchXml>" +

     

    " </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/Fetch);

    xmlHttpRequest.setRequestHeader(

    "Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader(

    "Content-Length", xml.length);

    xmlHttpRequest.send(xml);

     

    CheckXMLForError(xmlHttpRequest.responseXML);

     

    return xmlHttpRequest.responseXML;

    Wednesday, September 23, 2009 8:00 AM

Answers

  • Hi.

    Everything seems to be fine. I suggest you to build Fetch requests using this tool .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, September 23, 2009 10:13 AM
    Moderator
  • You have a wrong Fetch syntax.
    Must be like this:
    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>" +
    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    " <fetchXml>" +
    " &lt;fetch mapping='logical'&gt;" +
    " &lt;entity name='new_phase'&gt;" +
    " &lt;attribute name='new_phaseid'/&gt;" +
    " &lt;attribute name='new_name'/&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_teamid' operator='eq' value='" + teamId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;link-entity name='new_stageheader' from='new_stageheaderid' to='new_stageheaderid' alias='aa'&gt;" +
    " &lt;link-entity name='new_stagedetail' from='new_stageheaderid' to='new_stageheaderid' alias='ab'&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_projectscheduleid' operator='eq' value='" + projectScheduleId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/entity&gt;" +
    " &lt;/fetch&gt;" +
    " </fetchXml>" +
    " </Fetch>" +
    " </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/Fetch");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    
    
    Tuesday, October 20, 2009 10:17 AM
  • Pete,

    I'm sure you'll probably end up having to do this again in the future...

    To create fetchxml queries I use stunnware .  To convert it into an html encoded string, I replace the double quotes with single quotes, then run it through this site to HTML encode it .


    I think what Sergey changed for the syntax is:

    " <fetchXml xmlns='http://schemas.microsoft.com/crm/2007/WebServices\'>"
     +
    

    TO:

    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"
     +
    
    And switched your fetch and your fetchxml tags because they are backwards.

    It may be easier to copy and then modify a working fetchxml query for javascript .

    -- MSCRM Blogger

    Tuesday, October 20, 2009 11:38 AM

All replies

  • Hi. You have to code special symbols like < and > in the body of Fetch request. Try to use following code:

    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>" +
    " <fetchXml xmlns='http://schemas.microsoft.com/crm/2007/WebServices\'>" +
    " &lt;fetch mapping='logical' count='250' version='1.0'&gt;" +
    " &lt;entity name='new_phase'&gt;" +
    " &lt;attribute name='new_phaseid'/&gt;" +
    " &lt;attribute name='new_name'/&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_teamid' operator='eq' value='" + teamId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;link-entity name='new_stageheader' from='new_stageheaderid' to='new_stageheaderid' alias='aa'&gt;" +
    " &lt;link-entity name='new_stagedetail' from='new_stageheaderid' to='new_stageheaderid' alias='ab'&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_projectscheduleid' operator='eq' value='" + projectScheduleId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/entity&gt;" +
    " &lt;/fetch&gt;" +
    " </fetchXml>" +
    " </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/Fetch");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    
    CheckXMLForError(xmlHttpRequest.responseXML);

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, September 23, 2009 8:04 AM
    Moderator
  • That returned an "Unable to process request" error.

    Is the SOAPAction "Fetch" correct?

    Wednesday, September 23, 2009 10:01 AM
  • Hi.

    Everything seems to be fine. I suggest you to build Fetch requests using this tool .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, September 23, 2009 10:13 AM
    Moderator
  • You have a wrong Fetch syntax.
    Must be like this:
    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>" +
    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    " <fetchXml>" +
    " &lt;fetch mapping='logical'&gt;" +
    " &lt;entity name='new_phase'&gt;" +
    " &lt;attribute name='new_phaseid'/&gt;" +
    " &lt;attribute name='new_name'/&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_teamid' operator='eq' value='" + teamId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;link-entity name='new_stageheader' from='new_stageheaderid' to='new_stageheaderid' alias='aa'&gt;" +
    " &lt;link-entity name='new_stagedetail' from='new_stageheaderid' to='new_stageheaderid' alias='ab'&gt;" +
    " &lt;filter type='and'&gt;" +
    " &lt;condition attribute='new_projectscheduleid' operator='eq' value='" + projectScheduleId + "'/&gt;" +
    " &lt;/filter&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/link-entity&gt;" +
    " &lt;/entity&gt;" +
    " &lt;/fetch&gt;" +
    " </fetchXml>" +
    " </Fetch>" +
    " </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/Fetch");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    
    
    Tuesday, October 20, 2009 10:17 AM
  • Pete,

    I'm sure you'll probably end up having to do this again in the future...

    To create fetchxml queries I use stunnware .  To convert it into an html encoded string, I replace the double quotes with single quotes, then run it through this site to HTML encode it .


    I think what Sergey changed for the syntax is:

    " <fetchXml xmlns='http://schemas.microsoft.com/crm/2007/WebServices\'>"
     +
    

    TO:

    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"
     +
    
    And switched your fetch and your fetchxml tags because they are backwards.

    It may be easier to copy and then modify a working fetchxml query for javascript .

    -- MSCRM Blogger

    Tuesday, October 20, 2009 11:38 AM