top of page

How to Write a Dynamics 365 CE Plugin

  • Writer: Ryan Redmond
    Ryan Redmond
  • Mar 12, 2024
  • 6 min read

Updated: May 2

Robot teacher in a bright, tech-themed classroom pointing at a chalkboard with colorful code illustrating How to Write a Dynamics 365 CE Plugin, while students at desks with laptops follow along.

In this article, I'll take you step-by-step through how to write a Dynamics 365 CE Plugin from the ground up.


If you're already familiar with Dynamics 365 CE, the Power Platform, and the concept of Plugins but need a quick refresher on crafting one, you're in the right place.


To kick things off, we'll cover the prerequisites and setup, followed by a detailed walkthrough of an example Plugin, from coding to registration. By the end of this guide, you'll have a fully functional and working Plugin.


Let's dive in!


Getting Started with How to Write a Dynamics 365 CE Plugin: Prerequisites and Setup


Developing Plugins for Microsoft Dynamics CRM requires a solid foundation in several key areas. Before creating your first Plugin, ensure you have the necessary skills and tools in place.


Here's what you need to know and do to get started:


  • Object-Oriented Programming (OOP) Concepts: A fundamental understanding of OOP principles is essential. Plugins are developed using these principles, which include encapsulation, inheritance, and polymorphism, among others.


  • The C# Programming Language: Plugins for Dynamics CRM are primarily written in C#. A good grasp of C# and its syntax, data types, and error-handling mechanisms is crucial for successful Plugin development.


  • The NuGet Dependency and Package Manager: NuGet plays a significant role in managing the libraries and packages your Plugin might depend on. Familiarity with NuGet will simplify incorporating external libraries and maintaining your project.


Setting Up Your Development Environment


Before you can start writing your Plugin, you'll need to set up your development environment with the following:


  • .NET Framework SDK: Ensure you have the .NET Framework SDK installed, specifically version 4.6.2 or later. This SDK provides the necessary libraries and tools to develop, test, and deploy your Plugins.


  • Visual Studio: A robust integrated development environment (IDE) like Visual Studio is indispensable for Plugin development. We recommend using Visual Studio Professional or Enterprise 2022 for its comprehensive features tailored for developers. During the installation, include the "Desktop development with C#" workspace, which equips Visual Studio with all the necessary tools and libraries for developing desktop applications, including Dynamics CRM Plugins.


With the prerequisites met and your development environment ready, you can write your first Plugin for Dynamics 365 CE (CRM). The following sections will guide you through writing, signing, and registering your first Plugin.


An Example Plugin: Implementation to Registration


A Plugin moves through two stages: implementation and then registration. In the implementation stage, you write the code and logic for the Plugin.


The registration stage is used to register the Plugin in Dynamics and hook it into the system and events that will trigger it.


The Plugin we will develop will be triggered by creating an Account record.


The Plugin will create a Task activity that will remind the creator of the Account (End User that created the Account) to follow up one week later.


Implementing the Plugin


After completing the setup and prerequisites, open Visual Studio and create a new project with the class library type. Make sure you choose .NET Framework 4.6.2 as shown below:


Visual Studio new project window showing C# templates, with "Class Library" selected. Project name is "Opctura.Plugins".

Next, right-click on the project in Solution Explorer and select “Manage NuGet Packages”:


Visual Studio interface with C# code for a class file open. Menu options on the right, project structure visible on a white background.

This will open the NuGet package manager’s view to manage dependencies for the project. Make sure to navigate over to the “Browse” tab.


This will allow us to install the library that Microsoft provides to aid in developing Plugins for Microsoft Dynamics. Search for Microsoft.CrmSdk.CoreAssemblies package and install it in the project:


Visual Studio interface showing NuGet Package Manager with Microsoft.CrmSdk.CoreAssemblies selected. Options like install visible.

After installing the library, go to Solution Explorer, rename the Class1.cs file to FollowUpPlugin.cs, and open it.


This class will contain the code and logic for the Plugin. Copy and paste the below code in the class:


Code screenshot of a C# class implementing a plugin. It includes methods for creating a CRM follow-up task. Text has a white background.



Let’s walk through the different parts of the code:


  • The Plugin class implements IPlugin interface and implements the Execute method in it.


  • The ITracingService enables writing to the tracing log. You can see an example in the final catch block.


  • TheIPluginExecutionContext provides access to the context for the event that executed the Plugin.


  • The code verifies that the context InputParameters includes the expected parameters for the CreateRequest that this Plugin will be registered for. The Entity passed to the request will be available if the Target property is present.


  • The IOrganizationServiceFactory interface provides access to a service variable that implements the IOrganizationService interface, which delivers the methods you will use to interact with the service to create the task.


  • The ID of the account being created is found in the context OutputParameters and set as the regardingobjectid lookup column for the task.


Now, let’s compile the project to ensure we don’t have any errors. Right-click on the project and select Build:


Visual Studio interface showing C# code for a plugin. The Solution Explorer on the right highlights "Optrua_Plugins" with a context menu.

Monitor the output view and ensure that it succeeded as shown below:


Code displayed in Visual Studio with an error message in the Output window. The interface includes Solution Explorer and Properties panels.

Now that the Plugin is ready, the final thing we need to do before moving to the registration stage is to sign the project. Microsoft only accepts signed projects.


To do that, right-click on the project, go to properties and signing tab as shown below:


Visual Studio interface showing C# code with error message in the output panel. The setting is a programming environment.

Computer screen showing a software development interface with code and error messages indicating build failure. Tabs and panels visible.

In the Choose a Strong Name Key File dropdown, select <New…>. In the Create Strong Name Key dialog, enter a key file name and deselect the Protect My Key File with a password checkbox. Click OK to close the Create Strong Name Key dialog.


Visual Studio interface with code file and error log open. A pop-up for signing manifests with details and buttons is visible in the foreground.

Save the changes and finally re-build the project again.


Registering the Plugin

Now that the Plugin is compiled and signed, we can register. To do that, you can either use the registration tool from Dynamics Developer SDK (not recommended) or use a community-managed tool called XrmToolBox.


We recommend using XrmToolBox because it provides more functionality and is easier to use.


Download and install XrmToolBox (www.xrmtoolbox.com) -- it's a free download. Just extract the zip file into a folder and run XrmToolBox.exe.

Then, create a connection to your environment as shown below:


Interface showing six connection methods: Connection Wizard, Microsoft Login Control, Certificate, Connection String, OAuth/MFA, Client Id/Secret.

Select “Connection Wizard” from this screen and enter the appropriate information for your environment.


Connection setup window with URL input field, credentials checkbox, and service timeout set to 2 minutes. Buttons: Help, Back, Reset, Next.

Enter your environment name in place of “organization” in the Environment URL field shown above and click on next.


Login window titled "User credentials" with fields for domain, username, and password. Buttons: Help, Back, Reset, Next.

Enter the username (email address) and Dynamics 365 CE password and click on next to login.


After a successful connection is made to the environment, the below screen will display, and you can enter a friendly connection name and click on finish. This connection information can be used with the various tools that are part of XrmToolBox.


Dialog box with "Connection validated!" message. Options include naming, setting environment highlight, and browser. Buttons: Help, Back, Finish.

Now that we have the connection, we will install the “Plugin Registration” tool in XrmToolBox.


Note that the “Tools” tab in the main interface is not the same as the “Tool Library.” Choose "Open Tool Library" on the welcome page. This will list many different tools that you can install to help you.


Go to the Tool Library in XrmToolBox and search for “Plugin Registration”, as shown below.


A computer screen displays a plugin registration tool with a blue header, icons, and text detailing version updates. The background is white.

Install the “Plugin Registration” tool. Finally, open the tool and select the connection you have created in the previous step.



A software interface displaying a file tree with detailed text lists, mainly "Proxies" and "Plugins". Minimalist white background.

The above image shows all the Plugins already registered in the environment.


To register your own Plugin, click “Register” in the top menu bar and select “Register new Assembly.” Then, navigate to the project location in File Explorer, and inside it, navigate to the following path:


bin\\debug 


and find your compiled DLL file.


A computer screen showing a folder with multiple file names, some highlighted. The background has a list of plugins. The mood is technical.

Select open and “Register Selected Plugins”:


Computer screen showing a software interface with a list of plugins on the left, some highlighted in yellow. Details of a plugin are on the right.

Finally, we need to register a step to connect this Plugin to the Create Message for the Account table:


Software interface displaying numerous plugins in a list on the left and configuration details on the right. A task setup window is open.

Click on “Register new Step,” and you are done. If you get a message about specifying attributes and performance, choose “no.” The Plugin is now registered and will be called whenever an Account record is created. As specified, the Plugin will create a follow-up task on the account record.


Conclusion


For this article, our goal has been to equip you with the knowledge to write your first Plugin, enhancing your Microsoft Dynamics 365 environment with tailored functionality.


We provided a quick, no-frills walk-through for creating a Microsoft Dynamics 365 Plugin. We started with Plugin prerequisites and then walked through the creation, signing, and registration processes.


Tons of additional background, reference information, and other tutorials are available on Microsoft Learn.


If you need extra support or want to enhance your team's skills, Optrua has got you covered. Specializing in Dynamics CRM and Power Platform with a commitment to Agile development, we deliver custom solutions for your specific business challenges.


We offer a full range of CRM Services, from new customizations to improving your Dynamics 365 setup, we provide the essential technical and development expertise to realize your objectives. Partner with us to turn your vision into reality.

Join the 10,000+ people who get CRM tips in their inbox every month.

You're all set to receive updates and valuable insights.

bottom of page