Dynamics 365 paging with fetchxml


Dynamics 365 web service has limitation to return 5000 records in a single web service call. Many a 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 then 5000 record.

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)
                    {
                        objList.Add(entity);
                    }
                    // Check for morerecords, if it returns 1.
                    if (returnCollection.MoreRecords)
                    {
                        // Increment the page number to retrieve the next page.
                        pageNumber++;
                    }
                    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
                doc.Load(reader);
            }
            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;
                    attrs.Append(pagingAttr);
                }
                XmlAttribute pageAttr = doc.CreateAttribute("page");
                pageAttr.Value = System.Convert.ToString(page);
                attrs.Append(pageAttr);
                XmlAttribute countAttr = doc.CreateAttribute("count");
                countAttr.Value = System.Convert.ToString(count);
                attrs.Append(countAttr);
                StringBuilder sb = new StringBuilder(1024);
                StringWriter StringWriter = new StringWriter(sb);
                XmlTextWriter writer = new XmlTextWriter(StringWriter);
                doc.WriteTo(writer);
                writer.Close();
                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 help!

Comments

Popular posts from this blog

Tips 01) self introduction in an interview

Computer Science Terminology

Dynamics 365 Error #2 Object reference not set to an instance of an object