Define

In Dynamics365, the IExecutionContext interface is a very important concept that contains information about the current plug-in or workflow execution environment.

{{ < notice tip > }} Namespace: Microsoft.Xrm.Sdk

Assembly: Microsoft.Xrm.Sdk (in Microsoft.Xrm.Sdk.dll) {{ < /notice > }}

Syntax

public interface IExecutionContext

Properties

{{ < notice tip > }} The list below is ranked from highest to lowest based on my usual frequency of use. {{ < /notice > }}

NameDescription
UserIdGets the GUID of the system user for whom the plug-in invokes web service methods on behalf of.
ModeGets the mode of plug-in execution.
MessageNameGets the name of the Web service message that is being processed by the event execution pipeline.
InputParametersGets the parameters of the request message that triggered the event that caused the plug-in to execute.
OutputParametersGets the parameters of the response message after the core platform operation has completed.
PostEntityImagesGets the properties of the primary entity after the core platform operation has been completed.
PreEntityImagesGets the properties of the primary entity before the core platform operation has begins.
BusinessUnitId BusinessUnitIdGets the GUIDGUID of the business unit that the user making the request, also known as the calling user, belongs to.
CorrelationIdGets the GUID for tracking plug-in or custom workflow activity execution.
DepthGets the current depth of execution in the call stack.
InitiatingUserIdGets the GUID of the system user account under which the current pipeline is executing.
IsExecutingOfflineGets whether the plug-in is executing from the Microsoft Dynamics 365 for Microsoft Office Outlook with Offline Access client while it is offline.
IsInTransactionGets a value indicating if the plug-in is executing within the database transaction.
IsOfflinePlaybackGets a value indicating if the plug-in is executing as a result of the Microsoft Dynamics 365 for Microsoft Office Outlook with Offline Access client transitioning from offline to online and synchronizing with the Microsoft Dynamics 365 server.
IsolationModeGets a value indicating if the plug-in is executing in the sandbox.
OperationCreatedOnGets the date and time that the related System Job was created.
OperationIdGets the GUID of the related System Job.
OrganizationIdGets the GUID of the organization that the entity belongs to and the plug-in executes under
OrganizationNameGets the unique name of the organization that the entity currently being processed belongs to and the plug-in executes under.
OwningExtensionGets a reference to the related SdkMessageProcessingingStep or ServiceEndpoint.
PrimaryEntityIdGets the GUID of the primary entity for which the pipeline is processing events.
PrimaryEntityNameGets the name of the primary entity for which the pipeline is processing events.
RequestIdGets the GUID of the request being processed by the event execution pipeline.
SecondaryEntityNameGets the name of the secondary entity that has a relationship with the primary entity.
SharedVariablesGets the custom properties that are shared between plug-ins.

UserId

Gets the GUID of the system user for whom the plug-in invokes web service methods on behalf of.

  • Syntax
Guid UserId { get; }

Mode

Gets the mode of plug-in execution.

  • Syntax
int Mode { get; }
  • Enumerated value

In SdkMessageProcessingStepMode enumerated value:

public enum SdkMessageProcessingStepMode
{
 [System.Runtime.Serialization.EnumMemberAttribute()]
 Synchronous = 0,
  
 [System.Runtime.Serialization.EnumMemberAttribute()]
 Asynchronous = 1,
}

MessageName

Gets the name of the Web service message that is being processed by the event execution pipeline.

  • Syntax
string MessageName { get; }
  • Enumerated value
public static class MessageName
{
    public const string Create = "Create";
    public const string Update = "Update";
    public const string Delete = "Delete";
    public const string Associate = "Associate";
    public const string Disassociate = "Disassociate";
    public const string SetStateDynamicEntity = "SetStateDynamicEntity";
    public const string RetrieveMultiple = "RetrieveMultiple";
    public const string Retrieve = "Retrieve";
    public const string QualifyLead = "QualifyLead";
    public const string Assign = "Assign";
}

InputParameters 和 OutputParameters

InputParameters and OutputParameters are generally used in Action, they are used in conjunction with each other, one is for input and the other is for output.

  • InputParameters

Gets the parameters of the request message that triggered the event that caused the plug-in to execute.

  • OutputParameters

Gets the parameters of the response message after the core platform operation has completed.

  • Code Sample
using Microsoft.Xrm.Sdk;
using System;
namespace Blog.D365.Plugins.Account
{
    public class GetAccountNameByGuidAction : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            IOrganizationService serviceAdmin = factory.CreateOrganizationService(null);
            string param_1 = context.InputParameters["Param_1"].ToString(); // Reception parameters
            string param_2 = context.InputParameters["Param_1"].ToString();
            try
            {
                string result = string.Empty;
                if (string.IsNullOrWhiteSpace(param_1) || string.IsNullOrWhiteSpace(param_2))
                {
                    // Return error
                }
                
                // Logical processing...
                
                // The result is returned after processing
                context.OutputParameters["results"] = result;
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }
}

IsInTransaction

Gets a value indicating if the plug-in is executing within the database transaction.

  • Syntax
// true if the plug-in is executing within the database transaction; otherwise, false.
bool IsInTransaction { get; }

IsolationMode

Gets a value indicating if the plug-in is executing in the sandbox.

  • Syntax
int IsolationMode { get; }
  • Enumerated value

In PluginAssemblyIsolationMode enumerated value

[System.Runtime.Serialization.DataContractAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "8.1.0.239")]
public enum PluginAssemblyIsolationMode
{
 [System.Runtime.Serialization.EnumMemberAttribute()]
 None = 1,
 
 [System.Runtime.Serialization.EnumMemberAttribute()]
 Sandbox = 2,
}

PreEntityImages

  • Definitions

The PreEntityImages property contains the current values of the entity before the plugin or workflow is executed.

  • Function

During the execution of a plug-in or workflow, you can use PreEntityImages to access the raw data of an entity for comparison, validation, and other operations.

  • Example Scenario
    • In a plug-in that updates a customer entity, you can use PreEntityImages to access the customer’s original address information, compare it with the new address, and determine whether a change has occurred.

PostEntityImages

  • Definitions

The PostEntityImages property contains the current values of the entity after the plug-in or workflow has been executed.

  • Function

During the execution of a plug-in or workflow, you can use PostEntityImages to access the most recent data of the entity for the next step in processing.

Refer

  1. IExecutionContext Interface)
  2. Dynamics365-Apps-Samples/samples-from-msdn/HelperCode/OptionSets.cs