D365 – Create Plugin Template

How to develop a Plugin?

  • You need to have Visual Studio Community Edition: https://visualstudio.microsoft.com/free-developer-offers/
  • Create a Class Library project for C#.
  • Download required assemblies using NuGet Package Manager.
  • Add a reference to Microsoft.Xrm.SDK.dll. This assembly is available in SDK.
  • You can implement IPlugin Interface from referenced assembly.

Create a Project

  • Open visual Studio (2019 used in this example)
  • Create a new “Class Library (.NET Framework) project

  • Configure and create your new project.

  • A simple class library is created and you will notice that there is no main method, which means that you cannot directly run the code and it has to be called from other applications.
  • The following this are done:
    • Rename the Class library to “HelloWorld” and also renamed the file.

    • To install the assemblies, click Tools > NuGet Package Manager > Manage NuGet Package for Solution.

    • Browse and search for Dynamics 365 Plugin and install the following assemblies (select the Project and click Install):
      • Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool
      • Microsoft.CrmSdk.CoreAssemblies

    • This installation will create some folders in your project, which you can check by right clicking on the solution and Open Folder in File Location.

    • Right click on References > Add Reference > Browse to the package location and add “Microsoft.Xrm.Sdk.dll”

    • Also check if “System.ServiceModel” assembly reference is added as well.

  • Specify the following namespace:
    • using Microsoft.Xrm.Sdk;
    • using System.ServiceModel;

For Iplugin Interface add the Menthod

  • You can access the Iplugin interface as:

  • Paste this in your project. Which will look something like this:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using System.ServiceModel;

namespace Tech_Quantum

{

public class HelloWorld : 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

{

// 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;

}

}

}

}

}

Now your plugin template is ready.

Check the next post “D365 – Pug-in Writing Code” where we will move forward with writing the Plug-in.