Dynamics CRM CDS: The Benefit of SharedVariables

Have you ever heard about SharedVariables? SharedVariables is an object in IPluginExecutionContext that allows us to set Dictionary (Key-Value) data to be used in the next event execution pipeline. Before, this object was very powerful. We can share SharedVariables across entities as long in the same execution pipeline. But even now it is changed, SharedVariables is underrated to be used among developers.

So now we will see a sample of how to use SharedVariables. The requirements for this tutorial are we have PreOperation and PostOperation plugins that have been registered in the same entity.

using System;
using Microsoft.Xrm.Sdk;
namespace SharedVariables
{
    public class OrderPostOperation : IPlugin
    {
        public OrderPostOperation()
        {
        }
        public void Execute(IServiceProvider serviceProvider)
        {
            var context = (IPluginExecutionContext)serviceProvider
                .GetService(typeof(IPluginExecutionContext));
            throw new InvalidPluginExecutionException(context
                .SharedVariables["data"].ToString());
        }
    }
    public class OrderPreOperation : IPlugin
    {
        public OrderPreOperation()
        {
        }
        public void Execute(IServiceProvider serviceProvider)
        {
            var context = (IPluginExecutionContext)serviceProvider
                .GetService(typeof(IPluginExecutionContext));
            context.SharedVariables["data"] = "HELLO-WORLD";
        }
    }
}

After you register the plugin code above and create steps for it (in my sample, I register it into salesorder entity). Then this is the result:

Basically, SharedVariables is an object that we can pass in the same execution context. We can pass any object as long as we can serialize it.


So what is the benefit of using this object (SharedVariables)? Here is the list:

  1. If you think like me, I only use Post Operation to only call other’s entities/Web-Services. So with this logic, in Pre Operation I will do all the validation. If the result is yes, I will pass context.SharedVariables[“call-webservice”] = true then in PostOperation I only need to check if(!string.IsNullOrEmpty(context.SharedVariables[“call-webservice”])) then do the sending process.
  2. Have you ever needed retrieving complex data, then reuse in multiple business operations within same execution context? You can store the data in SharedVariables and reuse it. It will make your code more simple.

3 thoughts on “Dynamics CRM CDS: The Benefit of SharedVariables

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.