Tuesday, April 19, 2011

How to pass Parameters from one Plugin Step to Another in MS CRM 2011

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

this can be done by using SharedVariables property of IPluginExecutionContext.

//Register the PreEventPlugin for a pre-event and the
/// PostEventPlugin plug-in on a post-event.


public class PreEventPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

// Create or retrieve some data that will be needed by the post event
// plug-in. You could run a query, create an entity, or perform a calculation.
//In this sample, the data to be passed to the post plug-in is
// represented by a GUID.
Guid contact = new Guid("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}");

// Pass the data to the post event plug-in in an execution context shared
// variable named PrimaryContact.
context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString());
}
}

//next plugin
public class PostEventPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

// Obtain the contact from the execution context shared variables.
if (context.SharedVariables.Contains("PrimaryContact"))
{
Guid contact =
new Guid((string)context.SharedVariables["PrimaryContact"]);

// Do something with the contact.
}
}
}
}

No comments:

Post a Comment