If you’re seeking to customize Dynamics 365 and add new functionality, developing your own plugin is a great way to achieve it. Dynamics 365 provides a comprehensive set of tools that enable developers to write plugins that can handle various business operations. In this article, we will go through how to write a simple Dynamics 365 plugin, step-by-step.
What Are Dynamics 365 Plugins?
Dynamics 365 Plugins are custom code elements that operate on core CRM operation events like creating or updating records, deleting records, and so on. A plugin can either execute before or after the CRM event. In addition, it is necessary to define registering and plugin execution steps.
Before we dive into coding, let’s learn more about some Dynamics 365 Plugin’s essential definitions:
1. Plugin: This is the custom code that executes when the specified operation occurs.
2. Event Execution Pipeline: This pipeline controls the sequence of events in which plugins are executed for a particular user event.
3. Message: A message represents a CRM event, such as create, update, delete, and so on.
4. Execution Step: An execution step defines a plugin’s contextual information and registration settings, including its executing order.
Simple Steps to Develop a Dynamics 365 Plugin
Step 1: Create a New Project
Start by opening your Microsoft Visual Studio program and select “Create a new project” option. After that, choose the “Dynamics 365/CRM Plugin Library” project template and click “Next.” Next, provide a project name and select the appropriate CRM server version.
Step 2: Add a New Class
To begin writing the plugin logic, you’ll need to add a new class to the project. Right-click the “Plugins” folder and select “Add > Class” option. Name your class and make sure to add the IPlugin interface to it.
Step 3: Registering Plugin
To register a plugin, first, you need to sign in to your Dynamics 365 account. After that, navigate to the “Settings” section and select “Customizations,” then “Customizations” again, and choose the “Plug-in Registration” option.
Here, add a new step for the message you want to listen to and configure the registration settings like execution order and context.
Step 4: Writing Plugin Logic
Now comes the fun part- writing the plugin logic. Let’s consider an example. Suppose you want to create a plugin that automatically generates a new lead record when a new account record is created. You can achieve this by writing the following plugin code:
public class NewAccountPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
var target = (Entity)context.InputParameters[“Target”];
if (target.LogicalName == “account”)
{
var newLead = new Entity(“lead”);
newLead[“firstname”] = “New Lead”;
newLead[“statuscode”] = new OptionSetValue(1);
service.Create(newLead);
}
}
}
}
In this code, we are checking whether the targeted entity during the account creation is of ‘account’ type, and if it is, we are creating a new lead record and setting the status code to “New Lead.”
Step 5: Build and Debugging
Compile your plugin project to generate the plugin assembly and deploy it. Once you’ve deployed the plugin, open your Dynamics 365 account, create a new account record, and save it. Now check the lead section, and you should see a new lead record created automatically.
Wrapping Up
Developing a simple Dynamics 365 plugin does not have to be difficult. Follow these steps to create and deploy your own custom business logic to streamline your business processes. While this is just the beginning, it gives you insight into the Dynamics 365 plugin development process. Beyond that, the possibilities are endless. Happy coding!