To create a PostOperation Plug-In for Contact (refer to SDK): after a contact generated, the task to say hello also generated:
0: Turn on the Plug-in Tracing Log
1: Create a ClassLibrary with .Net FrameWork4.6.2
2: Add two NuGet Packages:
3: sign the Class: Project Properties – Signing
4: Replace the .cs with following Codes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace MyCRM1
{
public class HelloWorld : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Extract the tracing service for use in debugging sandboxed plug-ins.
// If you are not registering the plug-in in the sandbox, then you do
// not have to add any tracing service related code.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// 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);
// 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"];
// start to try the bussiness logic of Plugin
try
{
Entity followup = new Entity("task");
followup["subject"] = "Send e-mail to the new Contact.";
followup["description"] ="Follow up with the contact to say hello.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = context.PrimaryEntityName;
// Refer to the contact in the task activity.
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
string regardingobjectidType = "contact";
followup["regardingobjectid"] =
new EntityReference(regardingobjectidType, regardingobjectid);
}
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("MyCRM: Creating the task activity.");
service.Create(followup);
tracingService.Trace("MyCRM: SayHello."); // just for fun
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
// end of try the bussiness logic of Plugin
}
}
}
}
5: Save the code; Ctrl + B to generate the .dll (Under Build)
Find the MyCRM1.dll file under “bin/debug/”
6: Register Assembly .dll and step
a: Find “PlugIn Registration Tool” and run it
b: Create new connection:
c: Under “Register – Register New Assembly”
d: Register new Step
7: now can check the plug-in in D365
a: create a contact to see if say hello task generated
b: check the Plug-in Trace Log