Adding Paging Cookie in Retrieve Multiple Plugin

Problem Statement – While implementing certain 3rd party integration with external systems, we might have to write a plugin on Retrieve Multiple message of a certain entity. In case we end up modifying the OutputParameter property, CRM does not by default adds the Paging cookie.

Scenario – Instead of keeping CRM features, sometimes client may like to use external tools like Marketo (marketing automation system).

Even though due to performance issues , we should avoid writing a logic on Retrieve Multiple plugin sometimes that may be the only alternative. In certain cases, this will involve modifying the OutputParameter property of plugin context. The main issue that arises with this is, by default CRM does not implements paging cookie in the output entity collection.

Solution – The following code snippet shows how the paging cookie is added in the plugin

// sortedEntityCollection is the final entity collection that needs to be binded with Retrieve Multiple output.

// pageCollection is the entity collection with paging cookie embedded into it.

EntityCollection pageCollection = new EntityCollection();
if (qe.PageInfo.Count > 0)
int i = qe.PageInfo.Count, j;
for (j = i * (qe.PageInfo.PageNumber – 1); j < sortedEntityCollection.Entities.Count && j < (i * qe.PageInfo.PageNumber); j++)
if (j < sortedEntityCollection.Entities.Count)
pageCollection.MoreRecords = true;
pageCollection.TotalRecordCount = sortedEntityCollection.Entities.Count;
localContext.PluginExecutionContext.OutputParameters[“BusinessEntityCollection”] = pageCollection;