Plugins Development in Dynamics 365 CRM | Part 1 – Setting up Visual Studio Project

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!

Pre-Requisites

Here’s what you need to be have installed in order to proceed to writing a plugin –

  1. 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
  2. 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.

Scenario

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 –

  1. 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 –

  1. An account has a custom field called as Group Code.

  2. 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 –

  1. 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

  2. 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.

  3. Once I click on Create, an Empty Project with the standard Class1 will be created which will be ready for you to start writing.

  4. 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…

  5. Then, go to Browse and then search for Dataverse…

  6. You’ll find Microsoft.CrmSdk.CoreAssemblies which you can download. I usually choose a little older version than the current one.

  7. 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.

  8. 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.

  9. Once done, you can see that the references required have now been added to the project.

  10. 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.

  1. I’ll now extend the IPlugin interface to the class.

  2. 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.

  3. 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));




  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. Once you tick it, it’ll expose the area below and you’ll be able to create a new strong key name file.

  9. 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.

  10. Once I click OK, the .snk file will appear here.

  11. 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 –

  1. Dynamics 365 Storage Utilization | Dataverse Storage | Power Platform Admin Center
  2. Use Hierarchy in Roll Up Fields in Dynamics 365 CRM
  3. Filter records in a View owned by a Team you are a member of | Dynamics 365 CRM
  4. Get GUID of the current View in Dynamics 365 CRM JS from ribbon button | Ribbon Workbench
  5. Dynamics 365 App For Outlook missing on SiteMap in CRM? Use shortcut link [Quick Tip]
  6. Import lookup referencing records together in Dynamics 365 CRM | [Linking related entity data during Excel Import]
  7. Mailbox Alerts Hide/Show behavior in Dynamics 365 CRM
  8. Excel Importing Notes (Annotation) entity in Dynamics 365 CRM
  9. Enable/Disable the need to Approve Email for Mailboxes in Dynamics 365 CRM CE
  10. Call Azure Function from Dynamics 365 CRM using Webhooks
  11. Show Ribbon button only on record selection in Dynamics CRM
  12. Accessing multiple occurrences of a field in Business Process Flow using JS in D365 CRM

Thank you!!

2 thoughts on “Plugins Development in Dynamics 365 CRM | Part 1 – Setting up Visual Studio Project

    • Hi, can you try Dataverse instead of Microsoft keyword? They’ve updated the name lately and goes by searching Dataverse.
      Also, if you search by CoreAssemblies, you’ll be able to find it directly. Hope this helps!!

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.