CRM 2011 – Pass Data Between Plugins using SharedVariables

2007_microsoft_dynamics_crmEvery day I have new things to learn … and yesterday I had a problem about tho share variables between plugins in my CRM2011 project. MSCRM2011 and the execution pipeline provides the ability to pass data from one plug-in to another through an IPluginExecutionContext property called SharedVariables. This property is a collection of key/value pairs which developers can use to share data between plug-ins which are registered on both the pre and post events.

Here is the way to pass some paramters and get in between the steps in MS CRM 2011, while writing plugin.

Pre-Create Account Plug-in

if (context.InputParameters.ContainsKey("Target") && context.InputParameters["Target"] is Entity)
{
Entity target = context.InputParameters["Target"] as Entity;
if (target != null)
{
// some wacky data validation
string city = target.GetAttributeValue<string>("address1_city") ?? string.Empty;
int numEmployees = target.GetAttributeValue<int>("numberofemployees");
int accountCategory = target.GetAttributeValue<int>("accountcategorycode");

// city is auckland, numEmployees > 100, account category is preferred customer
bool updateRelated = city.Equals("Auckland", StringComparison.InvariantCultureIgnoreCase) && numEmployees > 100 && accountCategory == 1;
context.SharedVariables.Add("updatedRelated", updateRelated);
}
}

Post-Create Account Plug-in

if (context.InputParameters.ContainsKey("Target") && context.InputParameters["Target"] is Entity)
{
Entity target = context.InputParameters["Target"] as Entity;
if (target != null)
{
if (context.SharedVariables.ContainsKey("updatedRelated"))
{
bool updateRelated = (bool)context.SharedVariables["updatedRelated"];
if (updateRelated)
{
// additional logic to update related records of the Account
}
}
}
}

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...