Thursday, February 23, 2012

Get the Context instance of ms crm workflow and plugin

To get the context of ms crm is the first phase every MS CRM developer come across when going to develop a custom workflow or plugin. The context instance of workflow or plugin gives us the details of the entity on which entity record this workflow is running which helps us to fetch more details to lead the custom logic.

//Workflow
[CrmWorkflowActivity("My Test Workflow")]
public partial class MyTestWorkflow : SequenceActivity
{
        #region Designer generated code
        ---
        ---
        #endregion
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext context)
      {
         IContextService contextService = (IContextService)context.GetService(typeof(IContextService));                                  
         IWorkflowContext workflowContext = contextService.Context;
         Guid entityID = (Guid)context.OutputParameters.Properties["id"];
      }
}

//Plugin
public class Plugin:IPlugin
{
    public void Execute(IPluginExecutionContext context)
    {
        DynamicEntity entityInstance;
        if (context.InputParameters.Properties.Contains("Target") && 
               context.InputParameters.Properties["Target"] is DynamicEntity)
        {
             entityInstance= (DynamicEntity)context.InputParameters.Properties["Target"];             
        }                        
    }     
}

No comments:

Post a Comment