How to write Plugins on CreateMultiple and UpdateMultiple messages in DataVerse

Hello everyone and welcome to my blog. In today’s blog, I will discuss on how to write plugins on CreateMultiple and UpdateMultiple messages of DataVerse.

If you are not aware of the feature, Microsoft have recently released two new messages in the platform. As of the time of writing this blog, these messages are still in preview. You can learn more about them in my blog here.

Through my blog I share interesting tips and discuss on latest releases of Microsoft.NET technologies, Dynamics 365 and Power Platform, SharePoint and Client scripting libraries. Please subscribe to my blog to stay updated.

Coming back to the topic, let’s how you can write plugins on these messages. I will start with CreateMultiple.

Since this is a preview feature, make sure you have the latest version of Plugin registration tool available. You can download it from here.

Below is the Plugin code for plugin that I will register in the Pre-operation of CreateMultiple

public void Execute(IServiceProvider serviceProvider)
      {
         IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
         IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
         IOrganizationService service = factory.CreateOrganizationService(context.UserId);

         if (context.InputParameters.ContainsKey("Targets"))
         {
            EntityCollection targets = (EntityCollection)context.InputParameters["Targets"];

            foreach(Entity target in targets.Entities)
            {
               // change any property of the Entity record
               // example
               target["cref2_milestonetitle"] = $"Original title {(string)target["cref2_milestonetitle"]} changed inside plugin";
               
            }

            // you can also remove an entity record from here based on logic.
            // targets.Entities.Remove([find the item to remove])
         }
      }

The plugin context of CreateMutiple message is pretty simple. It has just one property – Targets which is of type EntityCollection. In the pre-stage, you can modify the properties of a record inside the collection or you can modify the collection to add/ remove certain records from the Targets collection.

Below is the sample code for a plugin registered in Post-operation. Precisely here also we have the same Targets parameter. In the post you can identify the Id of the created record in collection.

public void Execute(IServiceProvider serviceProvider)
      {
         IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
         IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
         IOrganizationService service = factory.CreateOrganizationService(context.UserId);

         if (context.InputParameters.ContainsKey("Targets"))
         {
            EntityCollection targets = (EntityCollection)context.InputParameters["Targets"];

            foreach(var target in targets.Entities)
            {
               Guid recordId = target.Id;
            }
         }
      }

Below illustration show how you can register a plugin on pre-operation of CreateMultiple message.

The message is in preview and not available for all tables as of now. To know which table support this message, you can follow my blog here.

The next is UpdateMultiple. For UpdateMultiple, it’s pretty much the same. Your plugin will have the same Targets parameter for the entities in UpdateMultiple batch.

Below illustration for UpdateMultiple plugin message registration.

Hope this helped. For similar interesting topics on Microsoft.NET and Power platform, you can subscribe to my blog.

Debajit Dutta
Business Solutions MVP