How to use Plugin registration tool to register dependent assemblies in DataVerse/ Dynamics 365 plugins.

Welcome to all my blog readers. Today’s topic will be a bit longer than my usual blogs. I will talk about a new feature introduced with Power Platform 2022 Release wave 1. It’s a feature you have been awaiting for especially if you are a Power Platform/ Dynamics 365 Developer.

Today I am will discuss about the support of dependent assemblies in Dynamics 365/ Dataverse plugins.

All of you working with erstwhile Dynamics 365 and now Power platform might agree to using third party or external assemblies with your plugins. And traditionally we have resorted to the unsupported ilmerge.exe utility to merge the dependent assemblies inside a single plugin assembly.

We no longer need to as Microsoft has now released the support of dependent assemblies and you can use plugin registration tool to register the dependent assemblies. Without any further delay, let’s see how we can do this.

For this example, I start with a .NET framework class library project and a Nuget package reference to Microsoft.CrmSdk.Core.Assemblies. Well no big deal in this. Everyone who has developed a plugin is aware of this.

Let’s explore with two possible scenarions

The Newtonsoft latest packages (13.x.x.x) have some issues with .Net framework 4.6.2. I have used 11.x.x.x versions for this demo.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

The next step is to create a plugin. Below is the code for my plugin class

public class PreAccountCreate : IPlugin
   {
      public void Execute(IServiceProvider serviceProvider)
      {
         IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

         IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

         Entity entAccount = (Entity)context.InputParameters["Target"];
         string accountName = entAccount.GetAttributeValue<string>("name");

         string accountNumber = (new GenerateAccountNumber()).Generate(accountName);

         entAccount["accountnumber"] = JsonConvert.SerializeObject(accountNumber);

         context.InputParameters["Target"] = entAccount;

      }
   }

Look for couple of lines above highlighted in bold and italic. The plugin will execute on Pre-create of account and will set the accountnumber field.

To get the accountnumber, I used the Generate function. The Generate function is in the class GenerateAccountNumber of HelperLibrary project.

I further use the JsonConvert.Serialize function of NewtonSoft.Json to serialize the account number.

The idea is to utilize both the dependent assemblies, one included as project reference and the other an external assembly.

When I deploy this plugin, I need to merge the HelperLibrary and Newtonsoft.json with the Plugin assembly. Traditionally we would have used the ilmerge utility and while it worked, it was unsupported. Let’s see the new approach.

With the latest version of Plugin registration tool, you now have the option to Register a package

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

Once you click on “Register New Package” option, you will get the below prompt.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

You need to specify a Plugin package file and Solution.

Solution refer to the Dataverse solution where you want to create the project. For the demo I have selected the “Common Data Services Default Solution“. The package will be created in the Dataverse solution you select here.

Now the most important thing – the Plugin package file. If you click on the file selector, you will be asked to select a nuget package file (.nupkg extension).

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

Traditionally we have always selected the assembly file (.dll) to register a plugin assembly. And this is where you will have your first challenge.

How to create a .nupkg file for your project? I will show all the steps in details.

Before that, one thing you should make sure is your project is using Nuget packages as package reference and not packages.config. If you are using packages.config, follow this link to migrate your packages.config to package reference.

Once it is done, follow the below steps to create the .nupkg file

The first and the most important step is to create a .nuspec file. This is a one time process only. You don’t need to repeat this every time.

To do this, open Developer command prompt for visual studio and navigate to the folder where where the .csproj file is located.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

Use the below command to create .nuspec file.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins
nuget spec -force <your csproj file>

Open the .nuspec file in an editor of your choice. This is how it should look when you open it.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

You can remove the optional tags like licenseUrl, projectUrl and iconUrl from here. The other information which are parameterized like your id, version, title, authors are derived from your AssemblyInfo.cs file.

Check out for the version in the assemblyinfo.cs file as this will be the version of the package when uploaded to DataVerse.

Below is the my modified version of .nuspec file.

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2022</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
   <files>
      <file src="bin\debug\Newtonsoft.json.dll" target="lib"></file>
   </files>
</package>

Most importantly check for the files tag added after closing metadata tag. This is where you go ahead and add the path to all the dependent assemblies.

Observe that I have included Newtonsoft.Json.dll here but not my HelperLibrary.dll. If you have any dependent assemblies which are not project references, you need to specify in the files tag. Each time you add a dependent assembly which is not a project reference, you to include in the nuspec file.

How do I include dependent assemblies which are project references? We come to that in a moment.

Save the file. The next step is to create the .nupkg file. For that execute the below command. The easiest way is to create the .nupkg file automatically when you build your plugin project.

To do this, Right click on your project in visual studio -> Project properties -> Build events

In the Post-build event command line section, use the below command

nuget pack $(ProjectPath) -Symbols -Properties Configuration=$(ConfigurationName) -IncludeReferencedProjects

I am using the nuget pack command. Additionally I have used the –IncludeReferencedProjects switch. This is to include the all the dependent assemblies which are project references.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

And that’s it. I now have the .nupkg file which you can now upload using the Plugin Registration tool. Plugin registration tool -> Register new package

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

Once registered, choose Display by Package option.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

And below is the structure you shall view.

As you can see, inside the package, there is the assembly and within the assembly we have the plugin class. The next step is to go ahead and register a plugin on Pre-create of account.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

That’s it. Now when I run the plugin, the account number is updated successfully. Remember I am fetching the account number from my HelperLibrary.dll assembly and then serializing it with Netwonsoft.json.

To update the assembly, you need to update the package. To update the package, select the package (not assembly) in your plugin registration tool and click on Update.

Before I end the post, if you remember we used the “Common Data Services Default Solution” in the Register new package screen. Below is the snapshot of the solution.

Support for dependent assemblies in Dynamics 365/ Dataverse plugins

Hope you learnt something new today. You will also like the below posts.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading