web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Request URL Too Long for fetchXml query types

(0) ShareShare
ReportReport
Posted on by

I'm using CRM 2016 web api with fetchxml query parameter, but my query is too long it's simulate the IN operator were the number of parameters passed in the IN list are 300 elements and some times will be more than that.

So my problem is the query are too big for a GET HTTP Request. I have tried to send the query in the http message body but that didn't work so what is the solution for this problem?

Here's a code snippet of the fetchxml query that I used:




    

    <fetch mapping="logical" distinct="true">
       <entity name="entity">
          <attribute name="new_classopportunityid" />
          <attribute name="new_trainingproduct" />
          <attribute name="new_gtgstatus" />
          <attribute name="new_scheduledstartdate" />
          <attribute name="new_scheduledenddate" />
          <attribute name="new_remainingnumberofseats" />
          <attribute name="new_liveclassroom" />
          <attribute name="new_maxlive" />
          <attribute name="new_xavieruniversity" />
          <attribute name="new_partnerlive" />
          <attribute name="new_blended" />
          <filter>
             <condition attribute="new_classopportunityid" operator="in">
                <value>001943ea-e263-e611-8158-00155d002810</value>
                <value>0071e4ea-bd9b-e611-8163-00155d002810</value>
                <value>00c32774-1c8f-e611-8161-00155d002810</value>
                <value>00d513fa-f0bb-e611-8169-00155d002810</value>
                <value>....</value>
                <value>....</value>
                <value>....</value>
             </condition>
          </filter>
       </entity>
    </fetch>

The CRM web api endpoint that I request is :

   

     GET http://<org>/api/data/v8.0/<entity>?fetchXml=<fetch mapping="logical" distinct="true">
           <entity name="entity">
              <attribute name="new_classopportunityid" />
              <attribute name="new_trainingproduct" />
              <attribute name="new_gtgstatus" />
              <attribute name="new_scheduledstartdate" />
              <attribute name="new_scheduledenddate" />
              <attribute name="new_remainingnumberofseats" />
              <attribute name="new_liveclassroom" />
              <attribute name="new_maxlive" />
              <attribute name="new_xavieruniversity" />
              <attribute name="new_partnerlive" />
              <attribute name="new_blended" />
              <filter>
                 <condition attribute="new_classopportunityid" operator="in">
                    <value>001943ea-e263-e611-8158-00155d002810</value>
                    <value>0071e4ea-bd9b-e611-8163-00155d002810</value>
                    <value>00c32774-1c8f-e611-8161-00155d002810</value>
                    <value>00d513fa-f0bb-e611-8169-00155d002810</value>
                    <value>....</value>
                    <value>....</value>
                    <value>....</value>
                 </condition>
              </filter>
           </entity>
        </fetch>

This is the response I got from the api.

    Error code: 414: HTTP/1.1 414 Request-URI Too Long  Response :  "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\"\"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\">\r\n<HTML><HEAD><TITLE>Request URL Too Long<\/TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text\/html; charset=us-ascii\"><\/HEAD>\r\n<BODY><h2>Request URL Too Long<\/h2>\r\n<hr><p>HTTP Error 414. The request URL is too long.<\/p>\r\n<\/BODY><\/HTML>\r\n" [] []

*This post is locked for comments

I have the same question (0)
  • Verified answer
    NatrajY Profile Picture
    3,040 on at

    You can use the batch request for executing large fetchxml requests.

    e.g.

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/$batch", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "multipart/mixed;boundary=batch_contactfetch");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var response = JSON.parse(this.response.substring(this.response.indexOf('{'),this.response.lastIndexOf('}')+1));
    			console.log(response.value);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    
    var body = '--batch_contactfetch\n'
    body += 'Content-Type: application/http\n'
    body += 'Content-Transfer-Encoding: binary\n'
    body += '\n'
    body += 'GET ' + Xrm.Page.context.getClientUrl()+'/api/data/v8.2/contacts?fetchXml=<fetch count="10" ><entity name="contact" ><attribute name="fullname" /></entity></fetch> HTTP/1.1\n'
    body += 'Content-Type: application/json\n'
    body += 'OData-Version: 4.0\n'
    body += 'OData-MaxVersion: 4.0\n'
    body += '\n'
    body += '--batch_contactfetch--'
    
    req.send(body);
    


  • Community Member Profile Picture
    on at

    Thanks, is there another easier way to do that? I'm using php as a client and there's no api helper to construct a batch request to the crm api service.

  • NatrajY Profile Picture
    3,040 on at

    This a standard POST request and JavaScript. I am not sure what you mean by "api helper". You should be able to use OAuth and JavaScript this js  retrieve the results.

  • Community Member Profile Picture
    on at

    Ok, I will try to create that request, but I'm afraid of parsing the response since it contains http requests boundaries.

  • Community Member Profile Picture
    on at

    Thank you man, your code save my life

  • Balasaheb Profile Picture
    700 on at

    Hi,

    We have facing same, we tried to use Natraj solution and it retrieve data but data is not proper. For example we have where condition and add the 500 contact in it and define count =1000 in fetchxml that we passed in body. Then it returns 1000 contact it did not apply filter of our original filter and get only those contact. 

    So we need only 500 contact because we mentioned in our where condition. 

    Can you have solution to resolve this issue.

    Thanks!

  • Vikas Negi Profile Picture
    140 on at

    We are facing issue to fetch more than 5000+ records with batch request using Web API.

    we use below link to use batch request:

    community.dynamics.com/.../223201

    In first batch request we get 5000 records, but when we call another batch request with paging cookie than we getting "{"Message":"The batch request must have a \"Content-Type\" header."}" error message.

    Anyone know how to fetch more than 5000+ records with batch request.

  • NatrajY Profile Picture
    3,040 on at

    Use the WebApi Client library as it is makes it easier to do these sort of requests -> github.com/.../Xrm-WebApi-Client

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans