Using Shared Variables in Plug-in and send an email inside the CRM User using Plugin
Shared variables are used to share data from one plugin to another plugin with the same message and the same entity.
Some variable data that is dynamically generated in the running context cannot be stored in any field in the form, that kind of data can be transferred from one plugin (Pre-Operation) to another plugin (Post-Operation) by using Shared variables.
What are Shared Variables in Plugins: -
Shared variables are used for passing the data between plugins registered on both pre and post-events. Thus instead of capturing /storing values in custom-made attributes, those variables can be stored in context variables.
Limitations of Shared Variables: -
- For plug-in registered in stage 20(pre-operation) or 40(Post-operation) of the plugin execution pipeline, to access the stored variables from stage 10(Pre-operation) registered plug-in that executes on create, update, delete, or by a RetrieveExchangeRateRequest, you must access the ParentContext.SharedVariables collection.
- For all other cases, IPluginExecutionContext.SharedVariables contains a collection.
- One cannot share variables between steps using different messages and/or entities, even if one triggers others.
Syntax: -
To set the Shared variable
context.SharedVariables.Add("key", Value);
key = Any string name , Value = Object data type
To Get the Shared Variable in Another Plugin
string Fullname = (string)context.SharedVariables["key"];
Scenario: -
Add the FullName to the shared variable from the pre-operation plugin and retrieve it from the post-operation plugin,
and send an email to that contact entity inside the CRM.
Plugin code for adding FullName to the Shared Variables
Note: - Create the step in pre-operation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace Plugin1
{
public class SetSharedVariable: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity ContactEntity = (Entity)context.InputParameters["Target"];
string Firstname = ContactEntity.Contains("firstname") ? (string)ContactEntity.Attributes["firstname"] : null;
string Lastname = ContactEntity.Contains("lastname") ? (string)ContactEntity.Attributes["lastname"] : null;
string FullName = Lastname + " " + Firstname;
// Setting Shared variables
context.SharedVariables.Add("FullName", FullName);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
Plugin code for retrieving the Shared Variables and Send an email inside the CRM
Note: - Create the step in post-operation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
namespace Plugin1
{
public class GetSharedVariables : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity ContactEntity = (Entity)context.InputParameters["Target"];
//Retrieving the Shared variables
string Fullname = (string)context.SharedVariables["FullName"];
string Email = ContactEntity.Contains("email") ? (string)ContactEntity.Attributes["email"] : null;
if (Email != null)
{
Entity email = new Entity("email");
Entity fromparty = new Entity("activityparty");
Entity toparty = new Entity("activityparty");
// creating Entityreference variables to add to the email as FROM and TO
fromparty["partyid"] = new EntityReference("systemuser", context.UserId);
toparty["partyid"] = new EntityReference("contact", ContactEntity.Id);
//Adding enitiy reference array to emial FROM and TO fields
email["from"] = new Entity[] { fromparty };
email["to"] = new Entity[] { toparty };
email["subject"] = "Welcome Email";
email["description"] = $"Hi {Fullname}, \n Welcome to Effigent Software Solutions";
email["directioncode"] = true;
email["regardingobjectid"] = new EntityReference("contact", ContactEntity.Id);
// Create an email Activity record
Guid EmailGuid = service.Create(email);
// Send email to Recipient
SendEmailRequest emailRequest = new SendEmailRequest
{
EmailId = EmailGuid,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse Response = (SendEmailResponse)service.Execute(emailRequest);
tracer.Trace(Response.ResponseName);
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
Comments
Post a Comment