Writing a Simple Dynamics 365 Plugin: A Step-by-Step Guide
Dynamics 365 is a powerful tool for managing your organization’s customer relationships. And with its extensive customization options, you can make the most of its functionality by creating plugins that tailor Dynamics 365 to your unique business needs. In this article, we’ll walk you through the steps to write a simple Dynamics 365 plugin.
What is a Plugin?
A Dynamics 365 plugin is a custom code that’s executed on a specific action taking place in Dynamics 365. It can manipulate data, enforce business rules, or perform actions like sending an email or creating a task. Plugins can be written in various languages like C# or VB.Net.
Step 1: Define the Purpose of Your Plugin
The first step is to decide the purpose of your plugin. What should it do when triggered? Identify the action that triggers your plugin and what changes your plugin should make to the data.
For example, let’s say you want to create a plugin that sends an email to the account manager whenever a new lead is created. In this case, your plugin should be triggered on Lead Creation and should send an email to the account manager.
Step 2: Create a Visual Studio Project
To create a plugin, you need to have Visual Studio installed. Open Visual Studio and create a new project.
Choose Dynamics 365 Plugin as project type and give it a name and location.
Step 3: Add Required Assemblies
When you create a new Dynamics 365 Plugin project, it creates a basic structure for you. However, you need to add some assemblies required for CRM development.
Right-click on the project and click on Manage NuGet Packages.
Search for Microsoft.CrmSdk.CoreAssemblies and install the latest version.
Step 4: Write the Code
Now, you can write the code for your plugin. In the Solution Explorer, expand the project and open the Plugin.cs file.
Add the required namespaces at the top of the file.
using System;
using Microsoft.Xrm.Sdk;
Next, define the plugin class and add the Execute method.
public class EmailNotificationPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Add your code here
}
}
Step 5: Add Plugin Registration Attributes
To register the plugin in Dynamics 365, you need to add some attributes to your plugin class. These attributes define the trigger and how your plugin should be registered.
Add the following attributes to your plugin class.
[PluginRegistration(
MessageName = “Create”,
EntityLogicalName = “lead”,
Stage = PipelineStage.PostOperation,
ExecutionMode = ExecutionMode.Synchronous,
PluginType = PluginType.Plugin,
DeleteAsyncOperation = true)]
These attributes mean that your plugin will be triggered on Lead Create, it should run synchronously, and it should be registered as a plugin.
Step 6: Write the Plugin Code
Now, you can add the actual code to your plugin. In our example, we want to send an email to the account manager when a new lead is created.
Add the following code to your Execute method.
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 entity = (Entity)context.InputParameters[“Target”];
if (entity.LogicalName == “lead”)
{
var email = new Entity(“email”);
email.Attributes[“subject”] = “New Lead Created”;
email.Attributes[“description”] = “A new lead has been created. Please review.”;
email.Attributes[“to”] = new EntityCollection(
new List
{
new Entity(“activityparty”)
{
[“addressused”] = “accountmanager@organization.com”,
[“participationtypemask”] = 2
}
});
service.Create(email);
}
}
This code checks if the input parameter is a Lead entity and creates a new email entity with the necessary attributes to send an email to the account manager.
Step 7: Build and Deploy
Finally, you need to build the project and deploy the plugin to Dynamics 365.
Right-click on the project and click on Build.
Navigate to the bin/debug folder of your project and copy the .dll and .pdb files.
Open Dynamics 365 and click on Settings > Customizations > Customize the System.
Click on the Plugins button and then click on New.
Fill in the required details and upload the .dll and .pdb files.
Save and Publish your changes.
Conclusion
Writing a simple Dynamics 365 plugin can enhance the functionality of your CRM system and simplify your business processes. With the straightforward steps outlined in this guide, you can create a plugin that sends email notifications for new leads or any other specific purpose. Remember always to test your plugins before deploying them to production.