Dynamics 365 RetrieveMultiple Plugin

Leave a comment

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:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.com | LinkedIn | Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *