Retrieve More than 5000 records using Fetchxml with C#

 EntityCollection result = new EntityCollection();
            result.Entities.AddRange(RetrieveAllRecords(orgService, crtypFecthcXml));
   public static List<Entity> RetrieveAllRecords(IOrganizationService orgService, string fetchXml)
        {
            fetchXml = fetchXml.Replace("<fetch ", "<fetch {0} ");

            var moreRecords = false;
            int page = 1;
            var cookie = string.Empty;
            List<Entity> entities = new List<Entity>();
            do
            {
                var xml = string.Format(CultureInfo.InvariantCulture, fetchXml, cookie);
                var collection = orgService.RetrieveMultiple(new FetchExpression(xml));
                if (collection.Entities.Count > 0)
                {
                    entities.AddRange(collection.Entities);
                }

                moreRecords = collection.MoreRecords;
                if (moreRecords)
                {
                    page++;
                    cookie = string.Format(CultureInfo.InvariantCulture, "paging-cookie='{0}' page='{1}'", System.Security.SecurityElement.Escape(collection.PagingCookie), page.ToString(CultureInfo.InvariantCulture));
                }
            }
            while (moreRecords);

            return entities;
        }

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#