Introduction of Shared Variables in MS CRM2011
Plug-in might not been used by most of us, as we are not aware of its
unique feature. This Shared Variables
will be useful for sharing the data during complex plug-in development by
sharing the data between plug-ins registered on both the pre and post events.
Any values can be stored in the plugin context of Pre-event will be available in the Post-event of the plugin. This way we can avoid storing
the values in a custom attribute. Certain data validation which needs to be
done at post-event can be achieved by passing the value from pre-event of the
plugin. This also helps for performing business logics like updating related
records asynchronously.
The below code snippet illustrates the usage
of Shared Variable. This sample explains
the data validation done at post-event operation based on the value received
from the pre-event operation where a flag is updated with a value at the
pre-event level and sent to the post-event through shared variable.
Pre-Create
(Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
{
if (pluginContext.InputParameters.ContainsKey("Target") && pluginContext.InputParameters["Target"] is Entity)
{
Entity targetEntity = context.InputParameters["Target"] as Entity;
if (targetEntity
!= null)
{
bool setFlag = false;
//Check data validation for
attributes
string country = targetEntity.Attributes["address1_country"];
OptionSetValue industryCode = (OptionSetValue)target.Attributes["industrycode"];
// Country is US and Industry Type is Accounting
are Preferred
if (country.ToLower()
== "us" && industryCode.Value == 1)
{
setFlag
= true;
}
pluginContext.SharedVariables.Add("flag", setFlag);
}
}
}
Post-Create
(Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
{
if (pluginContext.InputParameters.ContainsKey("Target") && pluginContext.InputParameters["Target"] is Entity)
{
Entity targetEntity = pluginContext.InputParameters["Target"] as Entity;
if (targetEntity
!= null)
{
if (pluginContext.SharedVariables.ContainsKey("flag"))
{
bool recievedFlag = (bool)
pluginContext.SharedVariables["flag"];
if (recievedFlag)
{
}
}
}
}
}
This method lets you to pass data between
plug-ins without having to customize the system by creating hidden fields on
entities.
Post is very help full thanks for this.
ReplyDelete