Skip to main content

Notifications

Dynamics 365 Community / Blogs / Abhishek Dhoriya's Blog / Dynamics 365 CRM Plugin Sha...

Dynamics 365 CRM Plugin Shared Variables – Explained with Example

Understanding Shared Variables in Dynamics 365 CE(CRM) Plugins

When developing plugins in Microsoft Dynamics 365 CE or CRM, there are instances where you need to transfer data from one plugin to another within the same transaction. Shared Variables provide an efficient solution to achieve this.

What Are Shared Variables in Dynamics 365?

In the Dynamics 365 or CRM environment, Shared Variables enable the exchange of data between plugins registered for pre-event and post-event stages in the execution pipeline. By using Shared Variables, developers can reduce unnecessary calls to the Dynamics 365 organization service and avoid creating additional custom fields solely for data transfer between plugins.
The Microsoft Dynamics 365 CRM platform, through its execution pipeline, facilitates this data exchange via the `IPluginExecutionContext` object’s `SharedVariables` property. This property is essentially a collection of key/value pairs, which developers can use to share relevant data across plugins executed in the same transaction, enhancing both performance and data coherence.

How Shared Variables Work?

The Dynamics 365 plugin framework allows custom data to be shared between plugins by using Shared Variables. This data-sharing mechanism is particularly helpful when there is a need to:
  • Pass information from a pre-event plugin to a post-event plugin in Dynamics 365.
  • Perform data validations that are only required during the post-event stage by using values derived from the pre-event plugin.
  • Avoid the creation of custom attributes solely for temporary data storage; instead, data can be stored in a context variable, accessible in the post-event stage.
Shared Variables simplify plugin data exchange by reducing the necessity of creating and managing custom attributes or performing redundant service calls, allowing for more efficient plugin design.

Key Benefits of Using Shared Variables in Dynamics 365 Plugins

  1. Data Transfer Across Plugin Stages: Shared Variables enable seamless data transfer from the pre-event to the post-event of plugins, ensuring a smoother workflow within the plugin execution pipeline.
  2. Streamlined Data Validation: Certain validations that need to occur during the post-event stage can leverage data passed from the pre-event, avoiding the need for separate data retrieval operations.
  3. Reduced Dependency on Custom Attributes: Instead of persisting values in custom attributes within the entity, developers can utilize Shared Variables, which are accessible across different plugin stages.

Limitations of Shared Variables in Dynamics 365 CRM

Despite their advantages, Shared Variables have certain limitations:
  • Scope Restrictions: For plugins registered at stage 20 or 40, accessing shared variables from a stage 10 plugin (for operations like create, update, delete, or RetrieveExchangeRateRequest) requires access through the `ParentContext.SharedVariables` collection.
  • Message and Entity Boundaries: Variables cannot be shared across different steps if they use different messages or entities, even if one triggers the other.

Practical Use Case and Demonstration

To see Shared Variables in action, refer to the demonstration video provided. It covers a detailed, step-by-step explanation of how and when to implement Shared Variables in MS CRM plugins, alongside practical examples to illustrate their functionality and effectiveness in real-world scenarios.

In conclusion, Shared Variables in Dynamics 365 plugins provide a powerful tool for managing data flow within the plugin execution pipeline, enhancing both efficiency and code clarity by facilitating streamlined communication across plugin stages. Understanding their usage, benefits, and limitations can significantly improve plugin design and performance in Dynamics 365 development.

Frequently Asked Questions on Dynamics 365 Plugin Shared Variables

1. What are Shared Variables in Dynamics 365 Plugins?
Answer: Shared Variables in Dynamics 365 are a collection of key/value pairs used to share data between plugins registered on different stages (typically pre-event and post-event) within the same execution context. This allows developers to transfer data across plugins without creating custom fields or making unnecessary calls to the Dynamics 365 organization service.

2. Why should I use Shared Variables instead of custom attributes?
Answer: Shared Variables provide a temporary data-sharing method within the plugin pipeline. By using Shared Variables, you can avoid cluttering your entity schema with custom attributes meant only for temporary data passing, which improves efficiency and maintains a cleaner data model.

3. How do I access Shared Variables in a Dynamics 365 plugin?
Answer: You can access Shared Variables via the IPluginExecutionContext interface using the SharedVariables property. For instance:
var sharedVariables = context.SharedVariables;
To add or retrieve data, simply work with this collection using the key for your data item.

4. Can I use Shared Variables across different message types?
Answer: No, Shared Variables are restricted to plugins within the same message type and entity. They cannot be used across different messages or entities, even if one message triggers the other.

5. What is a practical example of using Shared Variables?
Answer: Imagine you need to validate certain data at the post-event stage but compute that data during the pre-event stage. Instead of retrieving the data again in the post-event, you can calculate it in the pre-event and store it in a Shared Variable. The post-event plugin can then retrieve and use this variable, avoiding duplicate computations or data retrieval.

6. Are Shared Variables persisted in Dynamics 365?
Answer: No, Shared Variables are temporary and exist only within the current execution context. They are not persisted in the database, making them ideal for transferring transient data between plugin stages.

7. Can I use Shared Variables with multiple plugins in the same transaction?
Answer: Yes, Shared Variables are accessible by all plugins within the same transaction and execution context, allowing data to be passed between multiple plugins as long as they are part of the same pipeline.

8. What are the limitations of using Shared Variables?
Answer: Shared Variables cannot be shared across plugins registered for different messages or entities. Additionally, if a plugin is registered on stage 20 or 40 and needs to access Shared Variables from a stage 10 plugin, it must use ParentContext.SharedVariables to do so. Shared Variables are also limited to the current transaction and do not persist beyond it.

9. How do I add a value to Shared Variables in Dynamics 365?
Answer: You can add a value to Shared Variables using code like the following in your plugin:
context.SharedVariables["YourKey"] = yourValue;
This allows other plugins in the same transaction to retrieve the value using the key.

10. Is there a best practice for naming keys in Shared Variables?
Answer: Yes, to avoid conflicts, it’s best to use unique, descriptive keys for Shared Variables, often including a prefix related to your plugin's functionality. For example, “InvoiceValidation_DiscountCheck” is more descriptive and less likely to cause naming conflicts than a generic key like "discount".

11. Can Shared Variables be used with custom workflows or other Dynamics 365 components?
Answer: Shared Variables are specific to the plugin execution context within Dynamics 365 and are not available for custom workflows, actions, or other components outside the plugin pipeline.​​​​​​​

Comments