{"id":1994,"date":"2016-02-05T11:27:20","date_gmt":"2016-02-05T11:27:20","guid":{"rendered":"https:\/\/www.magnifez.com\/?p=1994"},"modified":"2020-11-05T12:22:15","modified_gmt":"2020-11-05T12:22:15","slug":"shared-variables-in-dynamics-365-customer-engagement-crm-plugins-advanced-plugin-concepts","status":"publish","type":"post","link":"https:\/\/www.magnifez.com\/shared-variables-in-dynamics-365-customer-engagement-crm-plugins-advanced-plugin-concepts\/","title":{"rendered":"Shared Variables In Dynamics 365 Customer Engagement (CRM) Plugins"},"content":{"rendered":"\n

<\/p>\n\n\n\n

In continuation of advanced plugin concepts part 1, in this post, we\u2019ll discuss shared variables in ms crm plugins and how to use them. We\u2019ll also discuss user impersonation in plugins and how we can use it.<\/p>\n\n\n\n

Also read: What is Microsoft Dynamics 365?<\/a><\/strong><\/em><\/p>\n\n\n\n

1. Shared Variables<\/strong><\/h2>\n\n\n\n

Microsoft Dynamics 365 Customer Engagement<\/strong> platform and the execution pipeline provides the ability to pass data from one plug-in to another through an IPluginExecutionContext<\/em> property called SharedVariables<\/em>. This property is a collection of key\/value pairs which developers can use to share data between plug-ins which are registered on both the pre and post events.<\/p>\n\n\n\n

Shared Variables are useful for sharing the data during complex Plug-in development by sharing the data between Plugins registered on both the pre and post events.<\/p>\n\n\n\n

Any value stored in the Plug-in context of Pre-event will be available in the Post-event of the Plug-in. This way, we can avoid storing the values in a custom attribute. Certain data validation which needs to be done at post-event can be achieved by passing the value from Pre-event of the Plug-in<\/p>\n\n\n\n

Test Case: <\/strong>For a demo, let\u2019s say We want to check the if the contact being created is duplicate i.e. contact exists with same first name and last name in Customer Engagement. If it exists, on post-operation, we want to mark the contact status duplicate.<\/p>\n\n\n\n

Below is a sample code where we are passing a flag if we found this contact duplicate from a pre-event plugin and retrieving the shared variables from the post-event plugin and change status:<\/p>\n\n\n\n

using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing Microsoft.Xrm.Sdk;\nusing Microsoft.Xrm.Sdk.Query;\nusing Microsoft.Xrm.Sdk.Client;\nusing Microsoft.Crm.Sdk.Messages;\n\nnamespace TestPlugin\n{\n public class PreEventPlugin : IPlugin\n {\n  public void Execute(IServiceProvider serviceProvider)\n  {\n  \/\/ Obtain the execution context from the service provider.\n  Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)\n  serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));\n  IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(context.UserId));\n  if ((context.InputParameters.Contains(\"Target\")) && (context.InputParameters[\"Target\"] is Entity) && context.MessageName.ToUpper() == \"UPDATE\")\n  {\n   Entity entity = (Entity)context.InputParameters[\"Target\"];\n   String sFirstName = entity.GetAttributeValue<String>(\"firstname\");\n   String sLastName = entity.GetAttributeValue<String>(\"lastname\");\n   \n   QueryExpression qe = new QueryExpression(\"contact\");\n   qe.Criteria.AddCondition(\"firstname\", ConditionOperator.Equal, sFirstName);\n   qe.Criteria.AddCondition(\"lastname\", ConditionOperator.Equal, sLastName);\n   EntityCollection enColl = service.RetrieveMultiple(qe);\n   if (enColl.Entities.Count >= 1)\n   {\n    \/\/ variable named isDuplicate\n    context.SharedVariables.Add(\"isDuplicate\", true);\n    }\n   }\n   }\n  }\n\n public class PostEventPlugin : IPlugin\n  {\n  public void Execute(IServiceProvider serviceProvider)\n  {\n  \/\/ Obtain the execution context from the service provider.\n  Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)\n  serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));\n  IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(context.UserId));\n  \n  if ((context.InputParameters.Contains(\"Target\")) && (context.InputParameters[\"Target\"] is Entity) && context.MessageName.ToUpper() == \"UPDATE\")\n  {\n   Entity entity = (Entity)context.InputParameters[\"Target\"];\n   \/\/ Obtain the contact from the execution context shared variables.\n   Boolean isDuplicate = (Boolean)context.SharedVariables[\"isDuplicate\"];\n   if (isDuplicate)\n   {\n    \/\/update status to duplicate\n    Entity en = new Entity(\"contact\");\n    en.Id = entity.Id;\n    en[\"statuscode\"] = new OptionSetValue(100000000);\/\/Duplicate (custom value)\n    service.Update(en);\n    }\n   }\n  }\n }\n}<\/pre>\n\n\n\n

Your plugin code should look like below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Steps to register plugin:<\/strong><\/p>\n\n\n\n

Step 1:<\/strong><\/p>\n\n\n\n

Connect plugin registration tool with your Customer Engagement instance and click on register and register new assembly:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 2:<\/strong><\/p>\n\n\n\n

Select the location of your plugin dll and check to select all checkbox and click an ok button at the bottom of the screen:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 3:<\/strong><\/p>\n\n\n\n

Once an assembly is registered, your plugins will look like below. Make sure both plugins are visible here.<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 4:<\/strong><\/p>\n\n\n\n

Right click on the first plugin and click register new step:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 5:<\/strong><\/p>\n\n\n\n

Register the pre-operation step as shown below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 6:<\/strong><\/p>\n\n\n\n

Again right click on the 2nd plugin and register the post-operation step as shown below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Testing:<\/strong><\/p>\n\n\n\n

Step 1: <\/strong><\/p>\n\n\n\n

We already have a contact in a system with full name Vishal grade as shown below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 2:<\/strong><\/p>\n\n\n\n

Create another contact with the same name as shown below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Step 3:<\/strong><\/p>\n\n\n\n

Upon creation, the status of contact will automatically change to duplicate as shown below:<\/p>\n\n\n\n

\"Shared<\/figure>\n\n\n\n

Limitation<\/strong><\/p>\n\n\n\n

For a plug-in registered in stage 20 or 40 of plugin execution pipeline, to access the shared variables from a stage 10 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 the collection. <\/p>\n\n\n\n

What this means is that if the Shared Variable is being set during Pre-Validation, you need to look at the Parent Context to find it when retrieving it in a Pre-Operation or Post-Operation step. It may not be in the immediate Parent Context either; you may need to check each parent\u2019s parent to find where the Shared Variable has been stored.<\/p>\n\n\n\n

Lastly, you cannot share variables between steps using different messages and\/or entities, even if one triggers the other. For instance, let\u2019s say that we have a plugin that executes when an Account is updated and that this plugin updates associated Contacts. Let\u2019s also say that we have another plugin that gets triggered when the Contact is updated, so we have a case where the Account update plugin triggers this Contact update plugin. You\u2019d see that the IExecutionContext.Depth<\/strong> for the Contact plugin is set to 2, but the Shared Variables you set in the Account plugin will not be available to the Contact plugin.<\/p>\n\n\n\n

Watch :<\/strong> Using Shared variables in Plugins.<\/p>\n\n\n\n

\n