Dynamics 365 Shared Variables – Share data between Plugins

Shared variables in Dynamics 365 plugins are essentially global variables that are accessible across all plugin steps and instances. These variables can be used to store data that needs to be shared across different steps of the plugin or across different instances of the same plugin.

Using shared variables in Dynamics 365 plugins provides several benefits. First, it allows you to share data across different plugin steps and instances, which can be very useful if you need to process multiple records in a batch or if you have complex logic that requires data to be shared across different steps. Second, shared variables can help you optimize your plugin performance by reducing the need to retrieve data from the database multiple times. Finally, shared variables can simplify your plugin code by reducing the need to pass data between different methods or classes.

To use the shared variable, you can simply reference it within your plugin code. For example, if you want to increment the counter every time your plugin executes, you can do this:

public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext executionContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext))
int counter = 0;
//Check existence of shared variable and fetch the value from executionContext
if (executionContext.ParentContext != null && executionContext.ParentContext.SharedVariables.ContainsKey("Counter"))
{
counter = (int)executionContext.SharedVariables.ContainsKey("Counter");
}
executionContext.SharedVariables.Add("Counter",counter++);
// your logic
}

Generally, you may have different plugins run for the same entity in different orders. If you want to check whether a plugin has performed a certain logic during its execution, you can set a shared variable and then read it in another plugin, as shown below.

Setting shared variable first time:

public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext executionContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext))
if(mycondition == true)
executionContext.SharedVariables.Add("MyFlag", true)
else
executionContext.SharedVariables.Add("MyFlag", false)
}

Reading it another plugin:

public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext executionContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext))
bool myFlag = false;
//Check existence of shared variable and fetch the value from executionContext
if (executionContext != null && executionContext.SharedVariables.ContainsKey("MyFlag"))
{
myFlag = (bool)executionContext.SharedVariables.ContainsKey("MyFlag");
}
//if(myFlag)
// your logic
//else
// your logic
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s