Workflow solution creation , registration and usage in CRM2011

In this blog I would like to explain some practical steps about the Workflow lifecycle in CRM 2011.
The steps includes 
1. Creation of the workflow solution ( C# solution ) and generation of the assembly 

2. Registering the workflow assembly for CRM usage3. Using the workflow assembly in CRM 

1. Creation of the workflow solution and generation of the assembly 

The workflow can be defined as group of activities that performs the required process.+

a) Creating the activities of the Workflow steps in the C# solution  Workflow activities are defined in the System.Activities.dll of .NETFramework, so we need to add this dll to the workflow solution.

Define the Class : The workflow activities that are used in CRM are derived from the "CodeActivity" base class. so define the workflow activity class derived from the "CodeActivity" class.    

eg : public class Sampleworkflow : CodeActivity {} Override the abstract methods of the CodeActivity class :
Execute() Method: 
CodeActivity class declares the Execute()  method as the abstract method. All the derived classes should define the execute() method in the derived classes i.e in the workflow activity classes.    eg: protected override void Execute(CodeActivityContext executionContext)
Some common attributes and code useful in the execute() method are:
1. IWorkflowContext (defined in Microsoft.Xrm.Sdk.Workflow.dll)  which is derived from the IExecutionContext:  contains the details about the CRM attributes and parameters that can be used in the workflow execution. we can get the IWorkflowContext as follows    IWorkflowContext context = executionContext.GetExtension();
    Using the IWorkflowContext , we can obtains the CRM context values like  current records GUID, , Current User's Guid, OrganizationName, OrganizationId etc., as follows:     getting the current record Guid : WorkflowContext.PrimaryEntityId;    getting the current Usee Guid : WorkflowContext.UserId;   
2. Organization Service and Organization Service Factory:    To execute any CRM SDK API's, we need to have the organization service or OrganizationServiceProxy. These objects can be created after getting the IOrganizationServiceFactory  object from the CodeActivityContext  and then by creating the service/serviceproxy by sending the user Guid to the Service factor        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension();                     IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
How to define and extract the Input and Output parameters required in the Workflow execution:  Defining the parameters: The input and output parameters for the workflow activity can be defined in the workflow activity class as follows:           a) String data type as input                  [Input("Account ID")]                  [Default("0")]                  public InArgument AccountName { get; set; }

Accessing the parameters : Input and output parameters can be obtained from the CodeActivityContext class passed to the execute() method.            string accountName = AccountName.Get(executionContext);           Where executionContext is the argument of type CodeActivityContext for the function execute()         b) Entity Reference type as input                    [Input("EntityReference input")]                    [ReferenceTarget()]                    [Default(, )]                    public InArgument  { get; set; }
        This data type can be accessed and assigned to the local variable of type Guid, within the execute() function as :                 Guid localGuid = (.Get(executionContext)).Id;
 After defining the execute() method with the required functionality for the workflow activity, compile and generate the workflow dll. 
This dll needs to be registered with the CRM. The steps that are to be followed for workflow registration into CRM 2011 will be available in my next blog on  Registering the workflow assembly for CRM usage


Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,