I recently needed to pass data from a pre-validation (stage 10, before transaction starts) plugin, to a different plugin that was within the transaction in the same execution pipeline. In other words, I needed to validate a contact’s address via a Web API, and the request had to be logged within CRM. It was important that the logged API request was never rolled back (in case of exception in other plugins), so it had to be done before the transaction.

Addressing this kind of requirement is easy - simply use SharedVariables, this is exactly why they are there, isn’t it? To pass data between plug-ins.

So I set a shared variable in my pre-validation plugin:

Setting the shared variable
1
pluginContext.SharedVariables.Add("AddressValidated", true);

And tried to read it in my other plugin:

Trying to read the shared variable
1
var addressValidated = (bool)pluginContext.SharedVariables["AddressValidated"];

But this wasnt working. AddressValidated was not in the dictionary.

After carefully reading the SDK page again, I discovered this little gem in the bottom:

For a plug-in registered in stage 20 or 40, to access the shared variables from a stage 10 registered plug-in that executes on create, update, delete, or by a RetrieveExchangeRateRequest, you must access the ParentContext.SharedVariables collection. For all other cases, IPluginExecutionContext.SharedVariables contains the collection.

Simply changing my second plugin to

Notice the additional ParentContext.
1
var addressValidated = (bool)pluginContext.ParentContext.SharedVariables["AddressValidated"];

made everything work!

Importance of looking into the details. So happy this was properly documented!!

The Guru