Question

I am attempting to use the SharedVariable of the IPluginExecutionContext between different calls to the same plugin. I have the following scenario:

The user is attempting to create a new Entity Record and the plugin has been triggered on the Pre stage. Based on some logic, I am setting a SharedVariable like so:

 var context = (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
 context.SharedVariables.Add("MySharedVariable", true);

I then attempt to update other records of the same entity like so:

var qe = new QueryExpression("new_myentity");
qe.Criteria.AddCondition("ecs_myfield", ConditionOperator.Equal,"someValue");
var results = service.RetrieveMultiple(qe);

foreach (var foo in results.Entities)
{
    //Do something to foo
    service.Update(foo);
}

I also have a plugin registered for Update on the Pre stage, however, I want to check MySharedVariable and do something else based on whether or not it is set.

In the Update, the context does not contain the key for 'MySharedVariable'. I have confirmed this by using the ITracingService.

Is there some restriction on passing shared variables between plugins that are run on different records?

The plugin execution mode for both the Create and Update is set to Synchronous and as already explained, both are registered on the Pre Operation stage

Was it helpful?

Solution

I don't use SharedVariables often, but I'm sure they are available in the same Execution Context (for example from a Pre Event to a Post Event for the same message on the same record).

They can't be used to share values between different plugins on different messages on different records (as in your case: set the value inside the Create of one record and retrieve the value inside the Update message of a different record)

For your situation I think it is preferable to use a custom entity to store the values, or create an additional attribute to the entity.

OTHER TIPS

Hi by looking at the scenario you explained.

I will not be able to test this my self. But If you change the Update plugin from Pre to Post.

If you change the update plugin from PRE-Operation to Post Operation. You will definitely get the SharedVariable in the execution context.

Pass Data Between Plug-Ins

CRM 2011 Plugins – Shared Variables

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top