/// <summary> /// RetrieveMultiple method override optimized for FetchExpression. Returns all pages using callback or 'yield' iterator /// </summary> /// <param name="query">A query that determines the set of record</param> /// <param name="callback">Optional function to be called for each record page</param> /// <returns>Entity set as 'yield' iterator</returns> public static IEnumerable <Entity> RetrieveMultiple(this IOrganizationService service, FetchExpression query, Action <EntityCollection> callback = null) { CheckParam.CheckForNull(query, nameof(query)); EntityCollection collection = new EntityCollection { MoreRecords = true }; /// For performance reasons it's better to load XML once XDocument document = XDocument.Parse(query.Query); while (collection.MoreRecords) { /// Paging start working if Page > 1 query.NextPage(document, collection.PagingCookie); collection = service.RetrieveMultiple(query); callback?.Invoke(collection); foreach (Entity entity in collection.Entities) { yield return(entity); } } }
public void FetchNextPage_NoPage_Test() { /// Setup string expectedCookie = "<cookie page=\"1\"><accountid last=\"{ E79B8B61 - 9658 - 460F - B6B5 - A11CECF9F872}\" first=\"{ D65C4096 - B356 - 4EDB - A6C6 - A12EDA3F34EF}\" /></cookie>"; string fetch = @"<fetch count='5000' no-lock='true' > <entity name='account' > <attribute name='name' /> </entity> </fetch>"; /// Act FetchExpression query = new FetchExpression(fetch); query.NextPage(expectedCookie); /// Assert XDocument document = XDocument.Parse(query.Query); string page = document.Root.Attribute("page").Value; string actualCookie = document.Root.Attribute("paging-cookie").Value; Assert.AreEqual("1", page); Assert.AreEqual(expectedCookie, actualCookie); }