Was trying to retrieve all records for an entity (Simple enough) and yet took some time (more than what I expected). So thought of documenting it, so that I do not have to spend time on it for syntax.

******************* Here how it goes ****************************

// Create an object of PagingInfo

//queryforContacts is an object for QueryExpression

PagingInfo queryforContacts.PageInfo = new PagingInfo();

int pageNumber =1;

EntityCollection finalCollection = new EntityCollection();
EntityCollection results = new EntityCollection();

do
{

// Set records to be retrieved per page
queryforContacts.PageInfo.Count = 5000;

queryforContacts.PageInfo.PagingCookie = (pageNumber == 1) ? null : results.PagingCookie;

// Increment page number
queryforContacts.PageInfo.PageNumber = pageNumber++;

results = serviceProxy.RetrieveMultiple(queryforContacts);
//Set retrieved records in a new EntityCollection
finalCollection.Entities.AddRange(results.Entities);
}
while (results.MoreRecords);

Hope it helps someone 🙂 ! 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s