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

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

    have you got any answer i am having the same problem

  • Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: unable to limit the records count after using setRequestHeader("Prefer", "odata.maxpagesize=5") in web api request

    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.

  • Michel Gueli Profile Picture
    Michel Gueli 982 on at
    RE: unable to limit the records count after using setRequestHeader("Prefer", "odata.maxpagesize=5") in web api request

    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>

  • Nagaraj M Profile Picture
    Nagaraj M 161 on at
    RE: unable to limit the records count after using setRequestHeader("Prefer", "odata.maxpagesize=5") in web api request

    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
    Michel Gueli 982 on at
    RE: unable to limit the records count after using setRequestHeader("Prefer", "odata.maxpagesize=5") in web api request

    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();

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans