Plugins are essential for extending functionality and implementing custom business logic in Microsoft Dynamics 365 CRM. It’s common for multiple plugins to share data when they’re involved in the same transaction. This is where Shared Variables come into play.
What is a Shared Variable?
A Shared Variable in MS CRM is a key-value pair stored within the IPluginExecutionContext
. It allows different plugins registered on the same event pipeline to share data. This is particularly useful when plugins need to communicate or coordinate actions without making additional database calls.
Why Use Shared Variables?
- Data Sharing: Allows for seamless data transfer between plugins.
- Reduces Redundant Operations: Eliminates the need for repeated queries or data retrieval, thereby improving performance.
- Simplifies Plugin Logic: It makes managing complex scenarios easier by separating responsibilities across different plugins.
Scenario: Implementing a Shared Variable in MS CRM Plugins
Let’s consider a scenario to understand how shared variables can be used effectively:
Scenario: Suppose you have two plugins:
- Plugin A: Executes the “Pre-Operation” stage of the Create event for a Contact entity.
- Plugin B: Executes on the “Post-Operation” stage of the same Create event.
Objective: Plugin A performs initial checks and determines if additional processing is needed. This decision is communicated to Plugin B, which then takes appropriate action.
Step-by-Step Implementation
1. Setting the Shared Variable in Plugin A
In Plugin A, we set a shared variable to indicate whether additional processing is required.
public class PreCreateContactPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Example logic: Determine if further processing is needed.
bool requiresFurtherProcessing = true; // This can be based on some custom logic.
// Set the shared variable to communicate with other plugins.
context.SharedVariables["RequiresFurtherProcessing"] = requiresFurtherProcessing;
}
}
2. Reading the Shared Variable in Plugin B
Plugin B reads the shared variable set by Plugin A and executes additional logic if necessary.
public class PostCreateContactPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Check if the shared variable has been set by Plugin A.
if (context.SharedVariables.Contains("RequiresFurtherProcessing") &&
(bool)context.SharedVariables["RequiresFurtherProcessing"])
{
// Execute additional logic as required.
// For example, send a notification or update another entity.
}
}
}
Explanation
- Plugin A: Executes first in the “Pre-Operation” stage and sets a shared variable named
"RequiresFurtherProcessing"
to indicate whether additional logic is needed. - Plugin B: Executes later in the “Post-Operation” stage. It checks for the shared variable and, if the condition is met, performs the additional processing.
Key Points to Remember
- Shared Scope: Shared Variables are only available within the same transaction context. Plugins running on different transactions cannot share variables.
- Data Type Compatibility: Ensure that the data type stored in the shared variable is compatible with how it will be used in subsequent plugins.
- Exception Handling: Always include proper checks (like
Contains
) before accessing shared variables to avoid runtime exceptions.
Shared Variables provide a flexible and efficient way to pass data between plugins in Dynamics 365 CRM. By using shared variables, you can streamline your plugin logic, reduce redundant operations, and improve the overall performance of your CRM system.
Use this approach whenever you need to coordinate actions across plugins, ensuring that your CRM customizations are efficient, maintainable, and scalable.
Same transaction means? Do you mean same message like “create”?
yes Nadikattu