In Dynamics 365, we have the ability to register a plugin when a retrieve multiple request is executed on an entity. Here we will go through creating a a Retrieve Multiple plugin.
First, create a new class library in Visual Studio:
- Microsoft.Xrm.Sdk
In this example, we will run the RetrieveMultiple when Accounts are displayed. We can invoke this by selecting accounts in a view or running an Advanced Find query. Let’s take a look at Active Accounts:
When running through Advanced Find, we can take a look at the FetchXML of the query. Go to the Advanced Find and select Download FetchXML:
The FetchXML looks like:
We will register our plugin on Post Operation. We will have access to the BusinessEntityCollection. Add the following code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace Carl.Crm.RetrieveMultiple { public class AccountMultiple : IPlugin { public void Execute(IServiceProvider serviceProvider) { Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); if (context.OutputParameters.Contains("BusinessEntityCollection")) { var businessEntityCollection = (EntityCollection)context.OutputParameters["BusinessEntityCollection"]; foreach (Entity e in businessEntityCollection.Entities) { // Add code } } else { throw new InvalidPluginExecutionException("Error"); } } } }
Strongly sign the assembly and build the solution.
Now, we will register the plugin:
The plugin will show as registered:
Now register a new step:
Running this in debug, we can see a list of all account objects is returned:
With each of the key/values of the record:
When running through accounts view, we get a query of type FetchExpression:
When running through Advanced Find, we get a query of type QueryExpression:
Note there is a list of entities that support RetrieveMultiple, with not all supporting both connected to the server and disconnected.
Also note, the plugin will fire even if there are no results returned from the query.
If we change the plugin to run as PreValidation or PreOperation, we do not get the BusinessEntityCollection returned.
PreValidation:
PreOperation:
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL
[…] quienes estén interesados en saber cómo desarrollar este plugin, accedan a este post de Carl Souza, un MVP neoyorquino que me fue de mucha […]
Can we set filtering attribute on post op retrievemultiple plugin so that plugin is only called when the attribute is part of the request?
Can we get Viewid in context of retrievemultiple plugin?
we have to add filter for some specific team users.
Thanks,
Vijendra