Plugin on Retrieve Multiple in CRM 2015.

Recently we had a requirement to filter sub grid on one of the forms. We thought of using Plugin on Retrieve Multiple for this.

Here we were showing Opportunity Sub Grid on Account form.

So we created a view for Opportunity that will be used as Sub Grid. Used it as Related Record Sub Grid so that we can have the GUID of the Account record being passed to the plugin.

The problem we faced à Sorting and Paging stopped working for the Sub Grid.

void IPlugin.Execute(IServiceProvider serviceProvider)
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression && context.MessageName == "RetrieveMultiple")
QueryExpression qe = (QueryExpression)context.InputParameters["Query"];
if (qe.EntityName == "opportunity")
string currentCustomerId = string.Empty;
ConditionExpression[] filters = qe.Criteria.Conditions.ToArray();
foreach (var filter in filters)
currentCustomerId = filter.Values[0].ToString();

FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
req.FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='opportunity'>" +
" <attribute name='name' />" +
"<attribute name='customerid' />" +
"<attribute name='estimatedvalue' />" +
"<attribute name='statuscode' />" +
"<attribute name='opportunityid' />" +
"<order attribute='name' descending='false' />" +
"<link-entity name='account' from='accountid' to='parentaccountid' alias='ad'>" +
" <filter type='and'>" +
" <filter type='or'>" +
" <condition attribute='accountid' operator='eq' uiname='aaass' uitype='account' value=' " + currentCustomerId + "' />" +
" <condition attribute='accountid' operator='under' uiname='aaass' uitype='account' value='" + currentCustomerId + "' />" +
"</filter>" +
"</filter>" +
"</link-entity>" +
"</entity>" +
FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)service.Execute(req);
context.InputParameters["Query"] = resp.Query;


Hope it helps..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Plugin on Retrieve Multiple in CRM 2015.”

Share your thoughts

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

You are commenting using your 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