Dynamics CRM Help

Properties to check for Plugin Execution Context

By on Feb 3, 2016 in Plugin | 0 comments

Dynamics CRM Plugin code is always executed in certain context. It very important to check few properties to make sure code is executing in correct context before processing. This helps avoid unexpected behavior.

// Obtain the execution context from the service provider.

IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));

MessageName

If you want plugin to execute for specific event like Create or update, check message name. It is also helpful when you want to execute different logic for different events in same plugin

if (context.MessageName == "Create")

{

//Do something else

}

Stage

Stage determines whether you want to execute your logic pre-operation or post-operation. In CRM there are 4 event pipeline stages which are available to register plug-ins. It is important if you are expecting to use ID for a record for newly created record, you will need to make sure your code is running in post-operation

if (context.Stage == 10)// Pre-validation

{

//Do something else

}

else if (context.Stage == 20)//Pre Stage

{

//Do something else

}

else if (context.Stage == 40) //Post Stage

{

//Do something else

}

PrimaryEntityName

PrimaryEntityName lets you check to make sure that your code is being invoked for the expected entity

If (Context. PrimaryEntityName!=”account”)

{

//Show message that not expected entity

}

InputParameters

The InputParameters property contains in the request message being processed. Plugin can access/modify this data in request. Target is the most common input parameter used in plugins. It is of type enitity. Example if a plugin is processing an account record in create event, you can access fields related to account record through target input parameter.

// The InputParameters collection contains all the data passed in the message request.

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)

{

// Obtain the target entity from the input parameters.

EntityReference entity = (EntityReference)context.InputParameters["Target"];

}

OutputParameter

the OutputParameters property contains the data that is in the response message, Please note that only oputput parameter is populyaed only for post event

// Refer to the account in the task activity.

if (context.OutputParameters.Contains("id"))

{

//Get Id

}

Post a Reply

Your email address will not be published. Required fields are marked *