Dynamix Academy

The Perfect Experience to learn Microsoft Dynamics 365

Dynamics 365 Developer Training - Step by Step Develop A Plugin From Scratch

Develop Dynamics 365 Plugin From Scratch

An article on Dynamics 365 CRM Plug-in Development Basics or Step by Step Develop a Plugin in Dynamics 365 from Scratch by Dynamix Academy. A plug-in is a .NET assembly that you can upload to the Common Data Service. Classes within the assembly can be registered to specific events (steps) within the event framework. The code within the class provides a way for you to respond to the event so that you can augment or modify the default behavior of the platform.

Step by Step: How to Write a Dynamics 365 plugin?

The process of writing a plug-in is:

  1. Create a .NET Framework Class library project in Visual Studio
  2. Add the Microsoft.CrmSdk.CoreAssemblies NuGet package to the project
  3. Implement the IPlugin interface on classes that will be registered as steps.
  4. Add your code to the Execute method required by the interface
    1. Get references to services you need
    2. Add your business logic
  5. Sign & build the assembly
  6. Test the assembly
    1. Register the assembly in a test environment
    2. Add your registered assembly and steps to an unmanaged solution
    3. Test the behavior of the assembly
    4. Verify expected trace logs are written
    5. Debug the assembly as needed

Sample Plugin Code Structure

// 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.  
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);
}

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

catch (Exception ex)
{
    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
    throw;
}
}

Things to keep in mind while Creating a plug-in

  • You just created your first plugin, and now you have to register it in Dynamics 365 Instance
  • You have note that you need to use Plugin Registration Tool to register the plugins
  • Keep in mind that plugins will never run on the front-end, they will only run on the back-end. It’s not enough to simply register a plugin assembly – you need to register a Message Processing Step for the plugin to work

Useful References:

Also find More on Dynamix Academy :

Also find Dynamix Academy on:

Related Posts