Dynamics 365 Paging With FetchXML


Dynamics 365 web service has a limitation to return 5000 records in a single web service call. Many times we get the requirement to fetch more than 5000 records to meet the business requirement.

Microsoft has provided the concept of paging where we can get the data returned in multiple pages. We can make use of paging and page cookie to get more than 5000 records.

The below code snippet will help you achieve this.

public List < Entity > GetRecordsByFetchXml(IOrganizationService service, String fetchXML) {
    List < Entity > objList = new List < Entity > ();
    try {
        // Define the fetch attributes.
        // Set the number of records per page to retrieve.
        int fetchCount = 500;
        // Initialize the page number.
        int pageNumber = 1;
        // Specify the current paging cookie. For retrieving the first page, 
        // pagingCookie should be null.
        String pagingCookie = null;
        while (true) {
            // Build fetchXml String with the placeholders.
            String xml = CreateXml(fetchXML, pagingCookie, pageNumber, fetchCount);
            // Excute the fetch query and get the xml result.
            RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest {
                Query = new FetchExpression(xml)
            EntityCollection returnCollection = ((RetrieveMultipleResponse) service.Execute(fetchRequest1)).EntityCollection;
            foreach(var entity in returnCollection.Entities) {
            // Check for morerecords, if it returns 1.
            if (returnCollection.MoreRecords) {
                // Increment the page number to retrieve the next page.
            } else {
                // If no more records in the result nodes, exit the loop.
                return objList;
                //  break;
    } catch (Exception ex) {
        throw ex;
    return objList;
public String CreateXml(String xml, String cookie, int page, int count) {
    XmlDocument doc = new XmlDocument();
    try {
        StringReader StringReader = new StringReader(xml);
        XmlTextReader reader = new XmlTextReader(StringReader);
        // Load document
    } catch (Exception ex) {
        throw ex;
    return CreateXml(doc, cookie, page, count);
public String CreateXml(XmlDocument doc, String cookie, int page, int count) {
    try {
        XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
        if (cookie != null) {
            XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
            pagingAttr.Value = cookie;
        XmlAttribute pageAttr = doc.CreateAttribute("page");
        pageAttr.Value = System.Convert.ToString(page);
        XmlAttribute countAttr = doc.CreateAttribute("count");
        countAttr.Value = System.Convert.ToString(count);
        StringBuilder sb = new StringBuilder(1024);
        StringWriter StringWriter = new StringWriter(sb);
        XmlTextWriter writer = new XmlTextWriter(StringWriter);
        return sb.ToString();
    } catch (Exception ex) {
        throw ex;
    return String.Empty;

To get the required output please invoke GetRecordsByFetchXml method and pass the fetchxml downloaded from advanced find.

Hope this helps!