locked
Error on executing fetchxml query through javascript RRS feed

  • Question

  • I'm trying to get this piece of javascript code to work, it's placed on an ISV button
    <Button Icon="/_imgs/ico_18_debug.gif" JavaScript="CallCrmService = function () {
        var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        xmlhttp.open('POST', '/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');
         var soapMessage = '&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;' +
            '&lt;soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' ' +
                'xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'&gt;' +
            GenerateAuthenticationHeader() +
            '&lt;soap:Body&gt;' +
            '&lt;Fetch xmlns=\'http://schemas.microsoft.com/crm/2007/WebServices\'&gt;'+
            '&lt;fetchxml&gt;'+      
                '&lt;fetch mapping=\'logical\' count=\'50\' version=\'1.0\' &gt;' +  
                  '&lt;entity name=\'contact\'&gt;' + 
                        '&lt;attribute name=\'contactid\' /&gt;' +             
                  '&lt;/entity&gt;'+
                '&lt;/fetch&gt;'+
            '&lt;/fetchxml&gt;'+
            '&lt;/Fetch&gt;&lt;/soap:Body&gt;' +
            '&lt;/soap:Envelope&gt;';    
        xmlhttp.send(soapMessage);debugger; 
            return xmlhttp.responseXML;
    }; 
    var result = CallCrmService(); 
    if (result != null)
    { alert(result.xml);}
    else { alert('no result');}">
    This generates the following trace message:

    >SOAP Request:
    Address:http://localhost/mscrmservices/2007/CrmService.asmx
    URL:2007/CrmService.asmx
    HttpHeader:SOAPAction: http://schemas.microsoft.com/crm/2007/WebServices/Fetch
    SOAP Xml:
    <?xml version='1.0' encoding='utf-8'?>
    <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    <soap:Header>
    <CrmAuthenticationToken xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;WebServices">
    <AuthenticationType xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">0</AuthenticationType>
    <CrmTicket xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">
    </CrmTicket>
    <OrganizationName xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">myOrg</OrganizationName>
    <CallerId xmlns="http&#58;&#47;&#47;schemas.microsoft.com&#47;crm&#47;2007&#47;CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>
    </CrmAuthenticationToken>
    </soap:Header>
    <soap:Body>
    <Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>
    <fetchxml>
    <fetch mapping='logical' count='50' version='1.0' >
    <entity name='contact'>
    <attribute name='contactid' />
    </entity>
    </fetch>
    </fetchxml>
    </Fetch>
    </soap:Body>
    </soap:Envelope>
    (replaced actual organization name with 'myOrg')

    This returns errorcode 500: 'Internal server error' and the following response XML:

    <soap:Fault>
    <faultcode>soap:Server</faultcode>
    <faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; Microsoft.Crm.CrmArgumentNullException: fetchXml ---&gt; System.ArgumentNullException: Value cannot be null.
    Parameter name: fetchXml
    --- End of inner exception stack trace ---
    at Microsoft.Crm.Exceptions.ThrowIfNull(Object parameter, String name)
    at Microsoft.Crm.Sdk.Crm2007.CrmService.Fetch(String fetchXml)
    --- End of inner exception stack trace ---</faultstring>
    <detail />
    </soap:Fault>
    But I can't see why it says the fetchxml is empty, because it isn't.
    Another weird thing: if i change the fetchxml node to fetchXml, I get 400: bad request as response and the message that the xml is malformed..

    I've spent the past 4 hours on this problem, so I hope something sees what is wrong here..
    Tuesday, January 19, 2010 4:28 PM

Answers

  • Try to use following:

    <Button Icon="/_imgs/ico_18_debug.gif" JavaScript="CallCrmService = function () {
        var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        xmlhttp.open('POST', '/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');
         var soapMessage = '&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;' +
            '&lt;soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' ' +
                'xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'&gt;' +
            GenerateAuthenticationHeader() +
            '&lt;soap:Body&gt;' +
            '&lt;Fetch xmlns=\'http://schemas.microsoft.com/crm/2007/WebServices\'&gt;'+
            '&lt;fetchxml&gt;'+     
                '&amp;lt;fetch mapping=\'logical\' count=\'50\' version=\'1.0\' &amp;gt;' + 
                  '&amp;lt;entity name=\'contact\'&amp;gt;' +
                        '&amp;lt;attribute name=\'contactid\' /&amp;gt;' +            
                  '&amp;lt;/entity&amp;gt;'+
                '&amp;lt;/fetch&amp;gt;'+
            '&lt;/fetchxml&gt;'+
            '&lt;/Fetch&gt;&lt;/soap:Body&gt;' +
            '&lt;/soap:Envelope&gt;';   
        xmlhttp.send(soapMessage);debugger;
            return xmlhttp.responseXML;
    };
    var result = CallCrmService();
    if (result != null)
    { alert(result.xml);}
    else { alert('no result');}">

    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)free countersLocations of visitors to this page
    • Marked as answer by Hanno Z Wednesday, January 20, 2010 12:36 PM
    Tuesday, January 19, 2010 7:34 PM
    Moderator

All replies

  • Try to use following:

    <Button Icon="/_imgs/ico_18_debug.gif" JavaScript="CallCrmService = function () {
        var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        xmlhttp.open('POST', '/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');
         var soapMessage = '&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;' +
            '&lt;soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' ' +
                'xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'&gt;' +
            GenerateAuthenticationHeader() +
            '&lt;soap:Body&gt;' +
            '&lt;Fetch xmlns=\'http://schemas.microsoft.com/crm/2007/WebServices\'&gt;'+
            '&lt;fetchxml&gt;'+     
                '&amp;lt;fetch mapping=\'logical\' count=\'50\' version=\'1.0\' &amp;gt;' + 
                  '&amp;lt;entity name=\'contact\'&amp;gt;' +
                        '&amp;lt;attribute name=\'contactid\' /&amp;gt;' +            
                  '&amp;lt;/entity&amp;gt;'+
                '&amp;lt;/fetch&amp;gt;'+
            '&lt;/fetchxml&gt;'+
            '&lt;/Fetch&gt;&lt;/soap:Body&gt;' +
            '&lt;/soap:Envelope&gt;';   
        xmlhttp.send(soapMessage);debugger;
            return xmlhttp.responseXML;
    };
    var result = CallCrmService();
    if (result != null)
    { alert(result.xml);}
    else { alert('no result');}">

    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)free countersLocations of visitors to this page
    • Marked as answer by Hanno Z Wednesday, January 20, 2010 12:36 PM
    Tuesday, January 19, 2010 7:34 PM
    Moderator
  • That's brilliant! I had already figured out that executing a fetchXml query only works with xml special characters replaced by unicode, but in ISV.config you have to do that for the entire soap message. But I never thought of replacing the & characters with &amp; in the actual fetch query as well! Now it works, thank you very much!
    Wednesday, January 20, 2010 12:36 PM
  • Hello,

    I am getting the similar kind of error

    "Server did not recognize the value of HTTP Header SOAPAction: http://schemas.microsoft.com/crm/2006/WebServices/Fetch."

    and below is my attached Xml Code.

    string fetchGetConceptDetails = @"<fetch mapping=""logical"">
                    <entity name=""new_udiconcept"">
                      <all-attributes/>
                      <filter> 
                        <condition attribute = ""new_name"" operator=""eq"" value=""" + ConceptName + @"""/>
                        <condition attribute = ""statecode"" operator=""eq"" value=""" + 0 + @"""/>
                      </filter>
                    </entity>
                   </fetch>";
    

    How and where should  i place the HTTP Header in my code sample?

     

     

    Monday, September 6, 2010 6:19 PM