Create Plugin from Scratch – D365

I will create a project from scratch and will show you how we can create a simple plugin step by step. Intension is to show how we can register plugin by creating a project and register assembly.

If you are looking for same, please follow below steps. I have also uploaded video here.

  • Open Visual Studio and click on File-> New project. I am using vs 2019 but you can use any version you are having.
  • Type ‘Class Library .net framework’ as show below and click on next
  • Provide Project Name , Location and select ‘4.6.2’ framework. Click on Create.

Project will be created. Now, Change the class name of the default file according to you. I am renaming it to CreateAccountPlugin

  • You will get a popup while replacing name, so click OK and file name will be renamed along with the class name.
  • Now, we have to install SDK from NuGet manager. So right click on project and click on ‘Manage NuGet Packages’

Now, under Browse add ‘Microsoft.CrmSdk.CoreAssemblies’ and select and click on ‘Install’.

  • Add below 2 using statement in your class file
    • using System.ServiceModel;
    • using Microsoft.Xrm.Sdk;
  • Implement the IPlugin in your class and add below code as given in the screenshot below.
public void Execute(IServiceProvider serviceProvider)
    {
        throw new NotImplementedException();
    }
  • Replace the code inside Execute method with below code. I have explained the code meaning in below screenshot.
// 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
    {
        // Plug-in business logic goes here.  
    }

    catch (FaultException<OrganizationServiceFault> ex)
    {
        throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
    }

    catch (Exception ex)
    {
        tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
        throw;
    }
}
  • Add your business logic in above code by replacing ‘// Plug-in business logic goes here’. Currently I am adding below sample code which will help us to test our plugin in the end.
    • In below example we are creating a task record on create of ‘Account’ entity record and link the task with the account. So as of now copy below code and check the code explanation in below screenshot.
// Create a task activity to follow up with the account customer in 7 days. 
Entity followup = new Entity("task");

followup["subject"] = "Send e-mail to the new customer.";
followup["description"] =
    "Follow up with the customer. Check if there are any new issues that need resolution.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = context.PrimaryEntityName;

// Refer to the account in the task activity.
if (context.OutputParameters.Contains("id"))
{
    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
    string regardingobjectidType = "account";

    followup["regardingobjectid"] =
    new EntityReference(regardingobjectidType, regardingobjectid);
}

// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the task activity.");
service.Create(followup);
  • Business logic explanation given below in screenshot.
  • Build the project.
  • Now, Right click on your project, click properties, select signing and check ‘Sign the assembly’, add new and uncheck ‘Protect my key file with a password‘ option.
  • Provide key name and click Ok and build your project to generate dll.
  • Navigate to your project folder by right clicking on your project and select ‘Open in File Explorer’. Here you can see your dll under bin\Debug\Plugindemo2.dll

Next step is to download Plugin registration tool from here to register plugin.

After download and installation, Open Plugin Registration tool and ‘Create New Connection’ by providing your D365 credentials and login.

  • After Login, It will open up your environment in the tool. Click on register and click on ‘Register New Assembly’.
  • Below screen will open up, Click on 3 dots and navigate to your path and select your dll which we have created in above steps.
  • After selecting, click on ‘Register Selected Plugin’ as given in below screenshot and your assembly will be registered. Sometimes you might get some error related to unsupported version, so I have selected 4.6.2 framework while creating project or you can change the version by navigating to your Project properties.
  • Now, your assembly will be registered and you will see your plugin under your assembly.
  • You can right click on plugin and add a new step according to your requirement.
    • Under Message it can be ‘Create’ or ‘update’ etc. Example : If you select create, then this plugin will run on create of record.
    • Under Primary entity, give entity name on which you want to run the plugin. In my example I have given Account and Create. So my plugin will run on Account record creation.
    • You can select operations like Prevalidation, PreOperation or PostOperation according to your requirement.
    • After giving all required details according to requirement, click on register new step.
  • After creating step, you can check your step under plugin as given below.

We have successfully created our project, registered assembly and plugin.

Now to test, you can create an Account entity record by going into your environment and after Account record creation you will see a ‘Task’ will be created and associated with the Account.

In this blog, I have created synchronous plugin but we can choose according to our requirement.

We have successfully created project and registered our first plugin. 🙂

Leave a comment