Microsoft Dynamics 365 provides many great out of the box (OOB) features. Nevertheless, many business scenarios demand enhancements to the OOB system that require implementing customized logic. This customization can be done in various ways, including Java Script, Custom Workflows, Custom Reporting, Scheduling Jobs, writing Plugins, etc.
Plugins are probably the most common and are a favorite among developers when it comes to implementing customized logic around entity actions (Create, Update, Delete, Associate, and Disassociate). A plugin is a class library that can be written in .Net platform-supported languages, including C# and Visual Basic. Class libraries – which are compiled into a DLL file (and more than one plugin can be part of a single DLL file – must be registered in the Dynamics 365 environment. Customized logic in plugins integrates with Dynamics 365 to perform desired business processes. In today’s blog, we’ll show you how that works…
Step 1: Getting Ready
First, we need an IDE to get started with plugin development. If you don’t already have them, download a Visual Studio and .Net Framework from Microsoft’s official website. The most recent version of Dynamics 365 can only support customized code built with Framework version 4.5.2 or later.
Next, if you don’t already have it, it’s time to Install Microsoft Dynamics 365 Customer Engagement Developer Guide (previously known as CRM SDK). As of Dynamics 365 v9, there is no longer a Microsoft-provided download for the Software Development Kit (SDK). However, by using either PowerShell or NuGet, we can still install the developer tools, assemblies, and code samples that are shipped as part of the SDK for Dynamics 365 v9.
Step 2: Writing a Plugin
We need a placeholder for our customized code, so choose a development language and then create a new Solution and Project in Visual Studio. Note that we cannot generate an executable file without having a Project and Solution.
Next add a “Class” and specify a class name for the plugin. This is where we would write a code to implement a business logic. Extend the class from Microsoft.Xrm.Sdk.IPlugin.
The SDK assemblies are available as NuGet packages that you can directly use in your Visual Studio projects. For information about using a NuGet package in Visual Studio, see Install and use a package in Visual Studio.
In the Create Strong Name Key box, enter a file name. Note that we will not be able to register our plugin to Dynamics 365 without having the Strong Name Key (SNK) created. SNK is system oriented, and when you move this development code on another machine, SNK needs to create again.
Everything is now set, so we are ready to implement business logic.
Step 3: Plugin Registration
The following developer tools in the SDK are available as NuGet packages:
- Code Generation
- Configuration Migration
- Package Deployer
- Plug-in Registration
- Solution Packager
To download developer tools, see Download tools from NuGet.
To register the plugin with Dynamics 365, we can use the Plugin Registration Tool. Enter all required connection information to establish a connection with the target Dynamics 365 organization. While registering new plugin step, we select PrimaryEntity and the Message actions (Create, Update, Delete, Associate, and Disassociate) for the plugin that is being Created/Updated.
Step 4: Pipeline Events
The next important attribute is Pipeline Stage, where we determine on which stage the plugin will fire. There are two main categories in which the event processes: Asynchronous (process runs in the background) and Synchronous (process runs immediately).
Step 5: Debug a Plugin
If implemented logic isn’t working as expected, we need to debug our code to identify the problem area and then reregister the plugin after fixing the code.
We can do this a few different ways, depending on whether you have an On-premises or Online instance of Dynamics 365.
- For On-premises Dynamics 365, we can debug plugin code by attaching Worker Process and/or Async Service in MS Visual Studio.
- For Online Dynamics 365, we can use the Plugin Registration Tool. Install Plugin Profiler in PRT using Install Profiler to get started. Using credentials, connect to the available instances of your organization in which your plugin exists and where you would like to debug it.
Select the plugin and then click on the Start Profiling option from the menu. Make necessary setting changes in the Profile Settings window. Choose Exception (Recommended) option and you will see an exception/error upon performing steps that we want to test. We can save thrown exception in the form of log file, which will be used to debug code. Choose Persist to Entity option to generate and profile in Dynamics 365 without throwing error. Later, we can use generated profile to debug code.
Once you have log file or profile created, open plugin code in Visual studio. Navigate to Debug menu and select Attach to Process to add PluginRegistration.exe that establishes the connection between code and Plugin Registration Tool. Don’t forget to add breakpoints to the key areas of code we want to test.
Go back to Plugin Registration Tool and select Plugin to test. Click Debug from the menu, select generated log file, and select the most recent .dll of the plugin we are testing. Click Start Execution and you will see debug triggered. Please refer to our detailed blog on Plugin Debugging for more information.
And that’s it! Happy Dynamics 365’ing!