Dataverse Introduction:

In this post, I will explain how to retrieve over 5000 records or large sets from Dataverse Using FetchXml, Paging Cookie, and More Records Flag.

Background
When using Dataverse or Dynamics 365 CE/CRM, the user can retrieve only 5000 records in a single fetch or query. This is a Dataverse Limitation.

Approach
To overcome this limitation, we can use “Paging Cookie” and have a flag called “More Records” to retrieve all records in a loop until the last page using the flag. Here is the Microsoft article to retrieve all records using c#.

First step we need to take Manually trigger a flow

Dataverse

Total Record  – To know the total record (For Analysis purposes only)

 

Page Number – To send the next page number on the request

FetchXml Paging Cookie – Dataverse returns the paging cookie as part of the response. This is Raw data and will be used to send a subsequent request.

Paging Cookie – Modified version of the original paging cookie, which will be sent as a request

More Records – Dataverse returns this flag as part of the response. This is used to determine to break the loop.

JSON For XML Parsing – This is just a template that used to transform the paging cookie to XML

Main Operation

We are using Do until control. This loop starts and continues by default until the More Records flags are false.

List Contacts

We are using Fetchxml query with page number and Paging Cookie. For Page 1, the Paging cookie will be empty.

The fetch statement has a paging cookie. Below is the Power Fx statement

if(equals(variables(‘PageNumber’),1),”,concat(‘paging-cookie=”’, substring(first(skip(split(string(xml(setProperty(variables(‘JSONForXMLParsing’),’a’,variables(‘PagingCookie’)))),'<‘),1)),2),””))

Incrementing the total count for each iteration. Finally, this variable has a total record count. This is used for Analysis purposes only. And other variables, the Page Number just increments for each iteration. This page number is used to send the request.

length(outputs(‘List_rows’)?[‘body/value’])

Dataverse has an attribute called PagingCookie as part of the response. Below is a variable to extract the Raw data from the response.

if(empty(outputs(‘List_rows’)?[‘body’]?[‘@Microsoft.Dynamics.CRM.fetchxmlpagingcookie’]),”,decodeUriComponent(decodeUriComponent(outputs(‘List_rows’)?[‘body’]?[‘@Microsoft.Dynamics.CRM.fetchxmlpagingcookie’])))

The paging Cookie variable extracts only the information needed to send the request.

 
if(empty(variables(‘FetchXml Paging Cookie’)),”,replace(substring(variables(‘FetchXml Paging Cookie’),
add(indexOf(variables(‘FetchXml Paging Cookie’),’pagingcookie=\”‘),14)),’\” istracking=\”False\” />’,”))

More Record – Flag to determine whether the Do until loop breaks or continue

if(empty(string(outputs(‘List_rows’)?[‘body’]?[‘@Microsoft.Dynamics.CRM.morerecords’])), false, outputs(‘List_rows’)?[‘body’]?[‘@Microsoft.Dynamics.CRM.morerecords’])

Final output

For  reference

For any help or queries contact us on info@crmonce.com or +91 9014146800