Tuesday, June 28, 2011

MS CRM 2011 Retrieve Limit

While working with MS CRM 2011 version I have discovered that if you are rtying to execute a query and retrieve more then 5000 records without specifying PageInfo parameters, you will only get the first 5000 records.
Here is how to approach this:

QueryExpression query = new QueryExpression("lead");
query.ColumnSet.AddColumns(leadColumnSet);
int pageNumber = 1;
RetrieveMultipleRequest multiRequest;
RetrieveMultipleResponse multiResponse;
do
{
query.PageInfo.Count = 5000;
query.PageInfo.PagingCookie = (pageNumber == 1) ? null : multiResponse.EntityCollection.PagingCookie;
query.PageInfo.PageNumber = pageNumber++;

multiRequest = new RetrieveMultipleRequest();
multiRequest.Query = query;
multiResponse = (RetrieveMultipleResponse)ServiceProxy.Execute(multiRequest);
leads.Entities.AddRange(multiResponse.EntityCollection.Entities);
} while (multiResponse.EntityCollection.MoreRecords);

2 comments:

  1. Thanks, recommended generic code. Helped me!

    AJ Sterman

    ReplyDelete
  2. Thanks for code...Its really helpfull to me.

    ReplyDelete