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)

unable to limit the records count after using setRequestHeader("Prefer", "odata.maxpagesize=5") in web api request

(0) ShareShare
ReportReport
Posted on by 161

Hi team ,

 i want to implement the pagination through webapi retrive call by passing fetchxml as query and i want to limit the records let's say 5 records per page but all the 5000 records are getting fetched in one call even i am including maxpagesize property in request header , can anyone kindly address this issue?

below is the code

function fetchquery(){
var query = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='account'>" +
"<attribute name='name' />" +
"<attribute name='accountid' />" +
"<order attribute='name' descending='false' />" +
"</entity>" +
"</fetch>";
var result = LargeDataRequest("accounts?fetchXml=" + query);
if (result != undefined && result != null && result.length > 0)
return result;
else
return null;
}
function LargeDataRequest(Query) {
debugger;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/"+Query, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.maxpagesize=5");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var recordCount = results["@odata.count"];
var firstname = results.value[i]["name"];
}
if (results["@odata.nextLink"]) {
var nextdatacount1 = results["@odata.nextLink"];
}
while (nextdatacount1 != "") {
var req1 = new XMLHttpRequest();
req1 = new XMLHttpRequest();
req1.open("GET", nextdatacount1, false);
req1.setRequestHeader("OData-MaxVersion", "4.0");
req1.setRequestHeader("OData-Version", "4.0");
req1.setRequestHeader("Accept", "application/json");
req1.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req1.onreadystatechange = function () {
if (this.readyState === 4) {
req1.onreadystatechange = "";
if (this.status === 200) {
var results1 = JSON.parse(this.response);
for (var i = 0; i < results1.value.length; i++) {
var recordCount = results1["@odata.count"];
var firstname = results1.value[i]["name"];
}
var resul = results1["@odata.nextLink"];
if (typeof (resul) == 'undefined') {
nextdatacount1 = "";
}
else {
nextdatacount1 = results1["@odata.nextLink"];
}
}
}
};
req1.send();
}

}
else { alert(this.statusText); }
}
};
req.send();
}

*This post is locked for comments

I have the same question (0)
  • Michel Gueli Profile Picture
    982 on at

    Maybe you can try this:

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=accountid,name&$orderby=name asc", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=5");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var accountid = results.value[i]["accountid"];

                   var name = results.value[i]["name"];

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

  • Nagaraj M Profile Picture
    161 on at

    Hi Michel thanks for the reply,

    but i want to pass fetchxml query because i want to isolate the webapi call from the query so that i can reuse the webapi request for every call instead of framing a separate call every time.

    is there any limitation that we can't use maxpagesize with fetchxml query execution?

  • Michel Gueli Profile Picture
    982 on at

    Try to use the count attribute in your fetchXml. Example:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" count="5">

     <entity name="account">

       <attribute name="name" />

       <attribute name="primarycontactid" />

       <attribute name="telephone1" />

       <attribute name="accountid" />

       <order attribute="name" descending="false" />

     </entity>

    </fetch>

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Take a look at the following blog regarding paging using fetchXml.

    This is the same as you are control the number of records per page.

    www.inogic.com/.../execute-fetchxml-using-web-api-in-dynamics-crm-2016

    Hope this helps.

  • Community Member Profile Picture
    on at

    have you got any answer i am having the same problem

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