To write a plugin for Dynamics 365 Customer Engagement (CE), you’ll need to follow these general steps:
- Set up your development environment: Make sure you have the necessary tools installed, such as Visual Studio and the Dynamics 365 CE SDK.
- Create a new plugin project: In Visual Studio, create a new Class Library project.
- Add references: Add references to the required Dynamics 365 assemblies (e.g.,
Microsoft.Xrm.Sdk.dll). - Write the plugin code: Implement your plugin logic in the form of a class that implements the
IPlugininterface. This interface requires you to implement theExecutemethod, which will be called when the plugin is triggered. - 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.
- 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