If you are a newbie to Dynamics 365 CRM and need to connect dots in getting your first plugin up and running, this post is for you! I’ve tried to summarized this in the shortest possible way. 😊 Hope this gets you going quickly!
Since you’re here, it’s presumed that you are told to learn or implement a Dynamics 365 CRM plugin with some business logic in mind based on the requirements. So, here’s how you get started in your learning journey!
Here’s what you need to be have installed in order to proceed to writing a plugin –
- Plugin Registration Tool – Required for your to connect to the Dynamics 365 environment and deploy your plugin on.
How to get Plugin Registration Tool: Download Plugin Registration Tool for Dynamics 365 CRM using PowerShell
- Microsoft Visual Studio – You’ll need to write your plugin in a Visual Studio IDE as it’s the preferred way to code your plugin and also to Version Control / Source Control.
Now, before you decide why you have to write a plugin, it is presumed that you have some high-level understanding as to why you are writing this plugin.
To keep it short, here are some points –
- Understand Plugin Execution Pipeline (Basically, it means when is the plugin supposed to run when you perform a certain operation) – https://carldesouza.com/dynamics-365-plugin-execution-pipeline/
Example Business Scenario
Here’s a quick example we’ll consider which we will implement using a plugin –
- An account has a custom field called as Group Code.
- So whatever is updated in this field should be updated on all the Child Contact records under the Account.
Now, let’s write a quick plugin to implement this.
Writing a Plugin – Visual Studio
Given that you want to start writing a plugin, you must also have understood some concepts –
- You’ll need to create a new .NET Framework Project in order to get started. I use Visual Studio 2022 (This was newly released at the time of writing this post)
So the type of Project required is a Class Library of .NET Framework
- Once you click Next, provide a relevant name to the Project. As a D365 Consultant working on multiple projects, you should be able to identify the library/assembly by the name as to which Org and what Module it belongs to.
In my case, I’m calling it CFT158SalesPlugins which is sufficient to give an idea of what org and what module this assembly is for.
- Once I click on Create, an Empty Project with the standard Class1 will be created which will be ready for you to start writing.
- Next, since Dynamics 365 CRM plugins extend IPlugin interface provided by Microsoft Dynamics, you’ll need to fetch references for the same.
Now, right-click on the Project itself and click on Manage NuGet Packages…
- Then, go to Browse and then search for Dataverse…
- You’ll find Microsoft.CrmSdk.CoreAssemblies which you can download. I usually choose a little older version than the current one.
- Make sure you have selected the correct one. Then, click OK.
Next, you get a chance to also look at the licensing terms before you Accept. Once you are OK, you can click on I Accept.
- If you have the Output window open in your Visual Studio, you’ll be able to see the progress. It only takes a few seconds for the references to be imported.
- Once done, you can see that the references required have now been added to the project.
- I’ll rename my Class1.cs to something that gives an idea of what the Plugin would do.
So, for example, I’m setting it to AccountUpdate.
Detailed post on Renaming or Deleting a Plugin in Dynamics 365 CRM
Now, that my class is renamed, I’ll also add the references to the Class File so that I can use the IPlugin interface. In most common Plugin scenarios, you’ll need Microsoft.Xrm.Sdk amongst other references too.
My personal practice is to include as shown below –
Setting Initial Methods
Now, let’s set a template of which we can start to code the plugin. Before we do that, some common Methods need to be added first.
- I’ll now extend the IPlugin interface to the class.
- As you choose the IPlugin, in Visual Studio, you’ll be prompted for some actions which will auto-complete the interface process. Look for the icon on the left hand side and expand the menu to find Implement Interface
Once done, the Execute method will be populated as below. Execute method is the first method from where the plugin execution starts.
Also, make sure the class is public so that we can register is successfully in the Plugin Registration Tool.
- Now, below are some helper methods which are required for the plugin to be registered on the environment.
You can copy from here:
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);
trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
- In order to understand what these methods are, you can simply hover over these and read the definition. If you don’t understand these right away, it’s not a problem. But, it’s recommended that you thoroughly understand the purpose behind each of these.
- Then, this should be the first method to call in order to establish connection with the Dynamics 365 CRM organization you are hosting this plugin on.
- Next, you’ll need to Sign the Assembly before it could be registered on the environment using Plugin Registration Tool.
Right-click on the Project and then choose Properties.
- Once in Properties, look for the Signing section on the left-hand side on the menu.
Notice that the Sign the assembly is not yet enabled.
- Once you tick it, it’ll expose the area below and you’ll be able to create a new strong key name file.
- When I click on new, I created a strong name key file –
Note: I’ve not password protected the Strong Name Key I’m creating but it’s up to you to maintain it if needed.
- Once I click OK, the .snk file will appear here.
- Finally, build the Project at this stage and we’ll move towards Registering this Plugin.
Now, at this point, we have established the code which is ready to be hosted on the Dynamics 365 CRM organization itself. Post this point, you’ll now code the logic which is supposed to be done by the plugin.
But first, let’s move to part 2 where you’ll first host this assembly in the Dynamics 365 CRM environment!
Registering Plugin in Dynamics 365 CRM environment
Here’s the Part 2 of the Blog: https://d365demystified.com/2021/12/31/plugins-development-in-dynamics-365-crm-part-2-registering-your-plugin/
Hope this was helpful! Here are some more Dynamics 365 posts which you might be interested in –
- Dynamics 365 Storage Utilization | Dataverse Storage | Power Platform Admin Center
- Use Hierarchy in Roll Up Fields in Dynamics 365 CRM
- Filter records in a View owned by a Team you are a member of | Dynamics 365 CRM
- Get GUID of the current View in Dynamics 365 CRM JS from ribbon button | Ribbon Workbench
- Dynamics 365 App For Outlook missing on SiteMap in CRM? Use shortcut link [Quick Tip]
- Import lookup referencing records together in Dynamics 365 CRM | [Linking related entity data during Excel Import]
- Mailbox Alerts Hide/Show behavior in Dynamics 365 CRM
- Excel Importing Notes (Annotation) entity in Dynamics 365 CRM
- Enable/Disable the need to Approve Email for Mailboxes in Dynamics 365 CRM CE
- Call Azure Function from Dynamics 365 CRM using Webhooks
- Show Ribbon button only on record selection in Dynamics CRM
- Accessing multiple occurrences of a field in Business Process Flow using JS in D365 CRM