Plug-in for SetState and SetStateDynamicEntity messages.

Hi, If we want to write a plugin that would run in case of state change of an entity record, we need to attach that plugin to both setstate and setstatedynamicentity messages.

Here is a sample plugin that would prevent an inactive contact record from getting activated.

Plugin has been registered against contact entity and two steps have been registered one on setstate and other on setstatedynamicentity messages on Pre event.

public void Execute(IPluginExecutionContext context)
{
// In case of SetState and SetStateDynamicEntity message InputParameter
// would contain EntityMoniker parameter 
Moniker entity = null;
if (context.InputParameters.Properties.Contains("EntityMoniker") &&
context.InputParameters.Properties["EntityMoniker"] is Moniker)
{
entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];
// Get the state to which record is to be changed
// If Active the record is being activated 
// If Inactive the record is being deactivated
string  state=
(string)context.InputParameters.Properties[ParameterName.State];
// Verify that the entity represents an account.
if (entity.Name == EntityName.contact.ToString() && state=="Active")
{
    throw new InvalidPluginExecutionException("Record can't be activated");
}
}
}
Bye..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

4 thoughts on “Plug-in for SetState and SetStateDynamicEntity messages.”

  1. Dear Nishant,
    can we do the Activate/Deactivate actions on multiple records and use the same plug-in?
    i want to prevent users from deactivating multiple records from the grid,
    but an error thrown telling that “the selected action was not completed for one or more records.”
    what message should i use?
    thank you

    Like

Share your thoughts

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 )

Google+ photo

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

Connecting to %s