Query Paging using Dynamics 365 WebAPI

Dominic Jarvis, 30 May 2018

When working with data in Dynamics 365, particularly large data sets, paging results is a necessity. It allows you to retrieve more than 5000 records, and retrieve records in manageable set sizes.

Paging using the WebAPI is a bit different to using the SOAP endpoint, so I’ve detailed below the structure of the requests that should be used and how to navigate to the next page from the response.

The paging works a little differently depending on whether you’re performing a straight OData query, or using FetchXML in your request.

Paging Regular Queries

When paging regular queries, there are a couple of things you need to know:
• Item count per page is specified using odata.maxpagesize=x.
• To execute the request for the following batch of records, simply open a new request to the URI specified in the @odata.nextLink attribute using the same headers as the initial request.



Paging FetchXML Queries

Increment page, retain count

When paging FetchXML queries, things are a little bit different:
• Item count per page is specified by setting the count attribute in the fetch node.
• The page number is specified in the page attribute of the fetch node. To retrieve the next page, update the page number to the next page.
• Unlike with the oData query, when paging through results there is no “@odata.nextLink” attribute returned, instead you must rely on the length of the returned array to determine if you have reached the last available page. There is also no “MoreRecords” attribute returned as either a response header or as part of the response body.

• If you page past the end of the entity collection, the value object will be an empty array.

  • o E.g. System contains a total of 15 accounts
  • - Fetch query is put together with page=1, count=10
  • - First page will have 10 records
  • - Second page will have 5 records
  • - Retrieving third page and onwards will succeed, but not return any records.