Fetching contributors…
Cannot retrieve contributors at this time
71 lines (53 sloc) 3.85 KB
title description ms.custom ms.reviewer ms.service ms.suite ms.tgt_pltfrm ms.topic applies_to ms.assetid caps.latest.revision author manager search.audienceType
Page large result sets with FetchXML (Developer Guide for Dynamics 365 Customer Engagement)| MicrosoftDocs
Read how you can page the results of a FetchXML query by using the paging cookie
Dynamics 365 (online)

Page large result sets with FetchXML


You can page the results of a FetchXML query by using the paging cookie. The paging cookie is a performance feature that makes paging in the application faster for very large datasets. When you query for a set of records, the result will contain a value for the paging cookie. For better performance, you can pass that value when you retrieve the next set of records.

FetchXML and xref:Microsoft.Xrm.Sdk.Query.QueryExpression use different formats for their paging cookies. If you convert from one query format to the other by using the xref:Microsoft.Crm.Sdk.Messages.FetchXmlToQueryExpressionRequest message or the xref:Microsoft.Crm.Sdk.Messages.QueryExpressionToFetchXmlRequest message, the paging cookie value is ignored. In addition, if you request nonconsecutive pages, the paging cookie value is ignored.

When you use the paging cookie with FetchXML, make sure that you use the correct encoding. The following example shows the correct encoding when using the paging cookie with FetchXML:

strQueryXML = @"  
<fetch mapping='logical' paging-cookie='&lt;cookie page=&quot;1&quot;&gt;&lt;accountid last=&quot;{E062B974-7F8D-DC11-9048-0003FF27AC3B}&quot; first=&quot;{60B934EF-798D-DC11-9048-0003FF27AC3B}&quot;/&gt;&lt;/cookie&gt;' page='2' count='2'>  
 <entity name='account'>  

FetchXML and the Paging Cookie Example

The following example shows how to use the paging cookie with a FetchXML query. For the complete sample code, see Sample: Use FetchXML with a Paging Cookie.


When not to use paging cookies

Paging cookies depend on the common case where each row returned represents a unique entity record. There are some queries you can construct using link-entity that will provide rows that combine data from the primary entity with related entities. This will result in multiple primary entity rows that refer to the same primary key value. If you depend on paging cookies in this situation you will get inconsistent results.

In this case, here are several strategies you can apply.

Write your FetchXml Query so that the primary entity ids are unique

If your query includes only one link-entity and doesn't include many-to-many relationships, you can usually make the related entity the primary entity in your query to resolve this.

Don't use the paging cookie

Rather than include the paging cookie in your FetchXml, simply update the page value. This will work but there will be some performance impact.

Split your query into multiple queries

You can compose multiple queries which you then join the results together in your code.

See also

Sample: Use FetchXML with a Paging Cookie
Building Queries with FetchXML
Fiscal date query operators in FetchXML
Using FetchXML
Page large result sets with QueryExpression