| Syed Junaid

A plugin is a custom business logic code that you can integrate with Dynamics 365 to modify the standard behaviour of the platform. It’s a fully customized code which you can write in .NET and register that .NET Assembly on various events after which it can be triggered on registered events. It can be registered on events like Update, Create etc which are also known as Sdk Messages.

Required tools to create a plugin are:

  1. Visual Studio 2017 (Or Later Version)
  2. .NET Framework 4.6.2
  3. Prior C# Experience

For creating a plugin we have to follow following steps:

  1. Open Visual Studio and create a Class Library (.NET Framework) project.
    Power Apps Maker portal
  2. Give a meaningful project name and use .NET Framework 4.6.2 as Framework and create the project.
    Power Apps Maker portal
  3. Now go to Manage NuGet Packages by right clicking the project.
    Power Apps Maker portal
  4. After that, search for CrmSdk Core Assemblies and install it and import those.
    Power Apps Maker portal
  5. Now we have all the packages required to develop a Plugin. Now we import the namespace which is required to get all functionality of Crm Sdk and for plugin development.
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    Power Apps Maker portal
  6. After that, implement the IPlugin interface and add this code to the Execute Method.
    // 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 ex)
    {
    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
    }
    catch (Exception ex)
    {
    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
    throw;
    }
    }

    In this code, First we used ITracing which is a tracing service which enables us to write a log, IPluginExecutionContext gets the context of the event which triggered the plugin. Then we check if we have an entity present in the context. IOrganizationService is the service reference that is responsible for create, update and all other functions for records in CRM. After these steps your code should look like this

    Power Apps Maker portal
  7. Now we can write our business logic in the try block. Like if we want to create an account record we can write this in a try block.
    //Create Account Entity Object
    Entity accountObject = new Entity("account");
    //Set Name, Email, Address Of That Account
    accountObject["name"] = "Junaid Zaidi";
    accountObject["emailaddress1"] = "syed.junaid@imperiumdynamics.com";
    accountObject["address1_line1"] = "45 Street, John Avenue";
    //Setting Parent Contact
    //Replace new Guid("A6AF4DAB-10C4-E911-A2E2-005056AE4389") with your GUID
    accountObject["primarycontactid"] = new EntityReference("contact", new Guid("A6AF4DAB-10C4-E911-A2E2-005056AE4389"));
    //Create Account Record. This function returns the GUID of the created account so that if you want to use it further in a code you can save it in a variable.
    service.Create(accountObject);
    Similarly, we have functions for Update, Delete, Retrieve, Associate etc which you can explore here.
  8. The final step is to sign the plugin. For that follow these steps:
    1. Right click the project and go to properties
      Power Apps Maker portal
    2. Go to signings and select sign the assembly
      Power Apps Maker portal
    3. Now choose New, write the key file name and uncheck the protect my key file with a password checkbox and proceed.
      Power Apps Maker portal

After these steps build your project and your plugin is ready for registration. Registration process of plugins can be found in Part 2 of this series.

Click here to learn more about Imperium's Dynamics Services.



About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.


More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting