How to write your first plugin in D365

To write a plugin for Dynamics 365 Customer Engagement (CE), you’ll need to follow these general steps:

  1. Set up your development environment: Make sure you have the necessary tools installed, such as Visual Studio and the Dynamics 365 CE SDK.
  2. Create a new plugin project: In Visual Studio, create a new Class Library project.
  3. Add references: Add references to the required Dynamics 365 assemblies (e.g., Microsoft.Xrm.Sdk.dll).
  4. Write the plugin code: Implement your plugin logic in the form of a class that implements the IPlugin interface. This interface requires you to implement the Execute method, which will be called when the plugin is triggered.
  5. Register the plugin: Use the Plugin Registration Tool or the Plugin Registration tool in the Power Platform admin center to register your plugin assembly and specify the message and entity it should trigger on.
  6. Deploy and test: Deploy your plugin to your Dynamics 365 CE environment and test it to ensure it behaves as expected.

Here’s a small sample code for a plugin that logs a message when an account record is created:

using System;
using Microsoft.Xrm.Sdk;

namespace SamplePlugin
{
    public class AccountCreatePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Check if the plugin is triggered by a create operation on the account entity
            if (context.MessageName.ToLower() == "create" && context.PrimaryEntityName.ToLower() == "account")
            {
                // Get the target entity from the execution context
                Entity accountEntity = (Entity)context.InputParameters["Target"];

                // Log a message
                string accountName = accountEntity.GetAttributeValue<string>("name");
                Console.WriteLine($"A new account '{accountName}' has been created.");
            }
        }
    }
}

Leave a comment

About Me
#D365 Business Apps, #Digital Transformation, #MS Industry Solutions.
#Technology Consulting and Architecture.
# Guitarist.