This is an older example of how to work with Plugins in Dynamics 2016. Most of the principles still apply today, but it is worth checking the official docs if you find something broken.
Basic Plugin Structure
In this example, the code inherits from CodeActivity and implements various uses of the SDK.
We define an input and an output both of which can be get and set.
On plugin execution, we then use the executionContext to implement a tracing service (the ability to log into the Dynamics trace logs (Settings> Plug in Trace Log).
We create a context in Dynamics using the IWorkflowContext and from that instantiate a new _service from the users id and an IOrganizationServiceFactory..
Between these we can run code on execution as a user and apply customer login to update information or create associated records etc.
Example Code
using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk; using System.Text; using System.Net.Mail; using Microsoft.Xrm.Sdk.Query; public class WorkingDueDate : CodeActivity { [Input("OffsetHourInt")] public InArgument OffsetHourInt { get; set; } [Output("NewData")] public OutArgument NewDate { get; set; } protected override void Execute(CodeActivityContext executionContext) { //Create the tracing service ITracingService tracingService = executionContext.GetExtension(); //Create the context IWorkflowContext context = executionContext.GetExtension(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); tracingService.Trace("Retrieving Case"); var primaryGuid = context.PrimaryEntityId; incident = service.Retrieve(incident.LogicalName, primaryGuid, true); NewDate.Set(context, primaryGuid); } }
The tracing Service is super important here, as it is the only way to get meaningful debug info from your instance. Make sure you don’t forget to use this!