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\nBelow 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\nYour plugin code should look like below:<\/p>\n\n\n\n <\/figure>\n\n\n\nSteps to register plugin:<\/strong><\/p>\n\n\n\nStep 1:<\/strong><\/p>\n\n\n\nConnect plugin registration tool with your Customer Engagement instance and click on register and register new assembly:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 2:<\/strong><\/p>\n\n\n\nSelect 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 <\/figure>\n\n\n\nStep 3:<\/strong><\/p>\n\n\n\nOnce an assembly is registered, your plugins will look like below. Make sure both plugins are visible here.<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 4:<\/strong><\/p>\n\n\n\nRight click on the first plugin and click register new step:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 5:<\/strong><\/p>\n\n\n\nRegister the pre-operation step as shown below:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 6:<\/strong><\/p>\n\n\n\nAgain right click on the 2nd plugin and register the post-operation step as shown below:<\/p>\n\n\n\n <\/figure>\n\n\n\nTesting:<\/strong><\/p>\n\n\n\nStep 1: <\/strong><\/p>\n\n\n\nWe already have a contact in a system with full name Vishal grade as shown below:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 2:<\/strong><\/p>\n\n\n\nCreate another contact with the same name as shown below:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 3:<\/strong><\/p>\n\n\n\nUpon creation, the status of contact will automatically change to duplicate as shown below:<\/p>\n\n\n\n <\/figure>\n\n\n\nLimitation<\/strong><\/p>\n\n\n\nFor 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\nWatch :<\/strong> Using Shared variables in Plugins.<\/p>\n\n\n\n