Common Context

This section describes the information that is available in the InputParameters and OutputParameters properties of IPluginExecutionContext. To understand the nature of this information, the relationship between an application making Web service calls and the context that a plug-in receives because of those calls must be described.

The following source code sample shows some typical calls to the Microsoft Dynamics CRM Web service.

[C#]
// Set up the CRM Service.
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the account object.
account account = new account();
account.name = "Fourth Coffee";

// Create the target object for the request.
TargetCreateAccount target = new TargetCreateAccount();
target.Account = account;

// Create the request object.
CreateRequest createRequest = new CreateRequest();
createRequest.Target = target;

// Execute the request.
CreateResponse createResponse = (CreateResponse)service.Execute(createRequest);
Guid accountID = createResponse.id;


Although this example uses Request/Response messages and the Execute method, you can use the other CrmService methods such as Create, Update, and Deleteto work with your business entities. The Microsoft Dynamics CRM system takes the information from those CrmService methods and transforms it intoRequest/Response messages that are processed by the event execution pipeline. The information on input/output parameters that follows still applies.

The following documentation relates the Web service calls in the example code to the context that a plug-in is passed. For the examples described herein, assume that a plug-in is registered to execute during the post-event of account creation so that the sample code causes the plug-in to execute.

Input Parameters


The InputParameters property bag contains all the information specified in the Request class whose message caused the plug-in to execute. The keys used to obtain values from the property bag are named according to the name of the Request class instance properties. An example of this is described next.

In the code sample, an account is created by using the CreateRequest class. The Target property of CreateRequest refers to a TargetCreateAccount object, which refers to an account object. This is shown in the following diagram.

CreateRequest and associated classes

When the plug-in is executed, the information that is in the Request object is passed to the plug-in in the InputParameters property of IPluginExecutionContext. For this example, the InputParameters property bag contains two key/value pairs. The first key is named "Target" and its associated value is the target of the request, namely the account object. The second key is named "OptionalParameters" and its associated value is the OptionalParameters instance property that is defined in the Request class from which CreateRequest is derived. In the sample code, no optional parameters were added to the CreateRequest, so that the optional parameters value in InputParameters is null.

The following figure shows an example plug-in that is being debugged in Visual Studio 2005.

Viewing the InputParameters property bag in a debugger

The figure shows the contents of the InputParameters property bag that has been passed in the context to the plug-in. Two key/value pairs are shown: Target andOptionalParameters.

The recommended way to specify these key names in code is to use the predefined names in the ParameterName class. For example, you can useParameterName.Target instead of "Target". Doing this reduces the possibility of a typing mistake in the key name when coding.

InputParameters contains Object types. Therefore, you must cast the property bag values to their appropriate types. The following sample plug-in code shows how to obtain the original account object.

[C#] DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target]

The account attributes can then be accessed in the plug-in.

[C#] string accountName = entity["name"]

Note The dynamic entity obtained from the InputParameters of the context contains only those attributes that are set to a value or null.

Output Parameters


For a post-event, the OutputParameters property bag contains the result of the core operation that is returned in the Response message. The key names that are used to access the values in the OutputParameters property bag match the names of the Response class instance properties.

CreateResponse class

In the source code example, the CreateResponse object contains the GUID of the new account instance after the Execute method is called. A post-event registered plug-in can obtain the GUID from the OutputParameters property bag by using the "id" key name or ParameterName.Id.

//[C#] 
Guid regardingobjectid = (Guid)context.OutputParameters[ParameterName.Id]

If a plug-in is registered for a pre-event, the OutputParameters property bag would not contain a value for the "id" key because the core operation would not yet have occurred.