We will be begin with creating a “CreatePlugin” that will create a Task activity to follow-up with the customer whenever a new lead is created in the system.
Step 1:
Create new project in Visual Studio > Create Class Library(.NET Framework)



Step 2:
Add required extension using nuget Solution.
1. Microsoft.Xrm.Sdk.2015:
Contains all functions for CRUD operations.
2. Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool:
Contains registration tool to register the plugin into MS CE.



Step 3:
Lets Code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Create_plugin
{
public class pluginClass : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organisation service reference.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService
(typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
// 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"];
try
{
// Create a task activity to follow up with the lead customer in 2 days
Entity followup = new Entity("task");
followup["subject"] = "Send e-mail to the new lead.";
followup["description"] =
"Follow up with the customer. Check with the lead for requirements.";
followup["scheduledstart"] = DateTime.Now;
followup["scheduledend"] = DateTime.Now.AddDays(2);
followup["category"] = context.PrimaryEntityName;
// Refer to the lead in the task activity.
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
string regardingobjectidType = "lead";
followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);
}
// Create the followup activity
var crm = service.Create(followup);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
Step 4:
Create a signing certificate



Step 5:
Build solution.


With this we have successfully created plugin dll. Refer to blog to know how to register plugin.
There are multiple services like Delete, Bulk create, retrieve, retrieve multiple etc. that are provided by Microsoft, which can be used to achieve business requirements.
Have any query regarding plugins, comment or get connected via LinkedIn. Will be happy to help.

