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;
}
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
Post a Comment