High-level overview of the process of writing a plugin in Dynamics 365

Power Dynamite
2 min readDec 18, 2022
  1. Set up your development environment: Install the necessary tools and components, such as Visual Studio and the Dynamics 365 SDK, on your development machine.
  2. Create a new Dynamics 365 plugin project: In Visual Studio, create a new project and select the “Dynamics 365 Plugin” template. This will create a basic plugin structure and files for you to start with.
  3. Define the plugin’s execution pipeline: In the plugin’s main class, specify the events that should trigger the plugin, such as the creation or update of a record, and the stages at which the plugin should execute.
  4. Implement the plugin’s logic: In the plugin’s main method, write the code that will be executed when the plugin is triggered. This can include operations such as reading and updating data, calling web services, and interacting with other Dynamics 365 components.
  5. Test and debug the plugin: Use Visual Studio’s debugging tools to test and debug your plugin as you develop it.
  6. Register the plugin: Use the Plugin Registration tool to register your plugin in Dynamics 365, specifying the events and stages at which it should execute.
  7. Deploy the plugin: Use Visual Studio to build and deploy the plugin to your Dynamics 365 environment.

Keep in mind that this is just a high-level overview of the process of writing a plugin in Dynamics 365. There are many additional considerations and details that you will need to take into account, such as handling errors, dealing with concurrency and transactions, and optimizing performance

Here are a few examples of plugin code in Dynamics 365:

  1. A simple plugin that logs a message to the trace log when an account is created:
using Microsoft.Xrm.Sdk;

namespace MyPlugins
{
public class AccountCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the execution context and tracing service
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Check if the target entity is an account
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Get the target entity
Entity entity = (Entity)context.InputParameters["Target"];

// Check if the entity is an account
if (entity.LogicalName == "account")
{
// Log a message to the trace log
tracingService.Trace("An account was created.");
}
}
}
}
}

2. A plugin that updates the name of an account based on its address:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

namespace MyPlugins
{
public class AccountUpdate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the execution context and tracing service
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Check if the target entity is an account
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Get the target entity
Entity entity = (Entity)context.InputParameters["Target"];

// Check if the entity is an account
if (entity.LogicalName == "account")
{
// Get the organization service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Check if the address fields are present
if (entity.Attributes.Contains("address1_line1") && entity.Attributes.Contains("address1_city"))
{
// Update the name field based on the address
entity["name"] = entity["address1_line1"] + ", " + entity["address1_city"];

// Update the entity
service.Update(entity);
}
}
}
}
}
}

--

--