Dynamics 365 RetrieveMultiple Plugin

3 Comments

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:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

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

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

3 Responses to Dynamics 365 RetrieveMultiple Plugin

  1. Can we set filtering attribute on post op retrievemultiple plugin so that plugin is only called when the attribute is part of the request?

  2. Can we get Viewid in context of retrievemultiple plugin?
    we have to add filter for some specific team users.

    Thanks,
    Vijendra

Leave a Reply

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