Monday, August 26, 2019

RetrieveMultiple plugins for both Unified Interface and classic UI in D365


I hope you have experience with writing plugins on RetrieveMultiple message. Even though you do not have experience with writing plugins on RetrieveMultiple message, this will help you when you write a new plugin.

Before the Unified Interface for Dynamics 365 comes in to play, it used the QueryExpression in RetrieveMultiple. But the Unified Interface Dynamics 365 uses FetchExpression. Meanwhile classic UI  uses the QueryExpression as usual.
So if your system already have plugins for RetrieveMultiple message, it won't work for UCI. Hence you may need to modify those to work for both UCI and classic UI.

Following sample code snippet will help you to write/update your plugins on RetrieveMultiple message.

if (context.MessageName == "RetrieveMultiple" && context.Stage == 20 context.Mode == 0 && context.InputParameters.Contains("Query") && ((context.InputParameters["Query"] is QueryExpression) || (context.InputParameters["Query"] is FetchExpression)))
{
 // For QueryExpression (For Classic UI)
 if (context.InputParameters["Query"] is QueryExpression)
 {
  var  queryExpression = context.InputParameters["Query"] as QueryExpression;
  var queryExpressionToFetchXmlRequest = new QueryExpressionToFetchXmlRequest()
  {
   Query = queryExpression
  };
  var queryExpressionToFetchXmlResponse = (QueryExpressionToFetchXmlResponse)service.Execute(queryExpressionToFetchXmlRequest);
 }
 // For FetchExpression (For UCI)
 else if(context.InputParameters["Query"] is FetchExpression)
 {
  var  fetchExpression = context.InputParameters["Query"] as FetchExpression;
  var fetchXmlToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest()
  {
   FetchXml = fetchExpression.Query
  };
  var fetchXmlToQueryExpressionResponse = (FetchXmlToQueryExpressionResponse)service.Execute(fetchXmlToQueryExpressionRequest);
 }
}

You can change the code inside the block as you wish to implement your logic and enjoy 👍