Write a Dynamics 365 CE Plug-in

Summary

This is a quick post demonstrating how to write a plugin in Microsoft Dynamics 365 Customer Engagement.

Create & Register a Plugin

Create a new Visual Studio project.

Select the .NET Framework Class library template.

The Assembly will need to be signed appropriately in order to deploy to the Server. Right-click to go into the project properties. Create a signature file.

..
The MicrosoftCrmSdk.CoreAssemblies requires the .NET 4.6.2 Framework. Change the target framework of the project to 4.6.2 if this is not correct.

The Target Framework required to use the CRMSDK is 4.6.2. Download this here: https://dotnet.microsoft.com/download/dotnet-framework/thank-you/net462-developer-pack-offline-installer

Add the Microsoft.CrmSdk.CoreAssemblies using Nuget package manager.

Once the project structure has been completed, add the following boiler plate code to the class file created at the start.

The code below simply capitalizes the First and Last name of the Contact. The Plugin will be registered on the Contact entity.

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace Eax360
{
    public class SetToUpper : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the tracing service
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Obtain the organization service reference which you will need for web service calls.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    // Set firstname and lastname to Uppercase

                    entity["firstname"] = entity["firstname"].ToString().ToUpper();
                    entity["lastname"] = entity["lastname"].ToString().ToUpper();

                    // Capture the column that forced the change
                    List<string> keys = new List<string>();

                    foreach (var result in entity.Attributes.Keys)
                    {
                        keys.Add(result);
                    }

                    // Insert changes to the description field
                    entity["description"] = String.Join(",", keys);
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                }
                catch (Exception ex)
                {
                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

Update the AssemblyInfo.cs as required.

using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following set of attributes.
// Change these attribute values to modify the information associated with an assembly.
[assembly: AssemblyTitle("EAX360.Activities.FirstPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("EAX360")]
[assembly: AssemblyProduct("FirstPlug")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible to COM components. If
// you need to access a type in this assembly from COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0e2bc506-8d66-4e6f-8c9d-bd56d2c29dbe")]

// Version information for an assembly consists of the following four values:
//
// Major Version Minor Version Build Number Revision
//
// You can specify all the values or you can default the Build and Revision Numbers by using the '*'
// as shown below: [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Compile the assembly and make sure to take note of the Assembly location.

Download the Plugin Registration tool from here: https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/download-tools-nuget?view=op-9-1

Once downloaded, start the Plugin Registration Tool. If you have multiple environments, select the correct environment that you’ll deploy to.

Register the new Assembly, by clicking on Register. Find and locate the Assembly.

The step above registers the Assembly with the Microsoft Dynamics 365 Customer Engagement platform. The final step is to register an event, message and filtering attributes.

Once all the steps above have been completed, and the assembly and SDK message steps have been registered successfully, you should be able to test the new Plugin.

If all went well, the Plugin should fire and the firstname, lastname should be capitalised.

You must be logged in to post a comment.