SharePoint 2013 - SPLessons

SharePoint State machine Workflow

Home > Lesson > Chapter 59
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SharePoint State machine Workflow

SharePoint State machine Workflow

A state machine workflow : provides a set of states. The workflow begins in an initial state and ends when it reaches the completed   state. Transitions between the states define the behavior. The occurrence of an event causes the workflow to transition to another state. One of the main benefits of the state machine workflow is the ability to define states and to define how the workflow moves from state   to state. You can easily include looping and re-execution of states in a state machine workflow. It is difficult, and potentially not possible, to   do this in a sequential workflow.

Create a state machine workflow using Visual Studio

Step 1

Create "RequestLeaveForm" List inside SharePoint And Create the Columns as we Required

Step 2

To create an Empty SharePoint Project
  1.  Start Visual Studio 2012 in administrator mode.
  2. On the File menu, point to New, and then click Project. If Visual Studio is set to use Visual c# development,settings, on the File menu, click New Project.
  3. In the New Project dialog box, select the SharePoint Soluction node.
  4. In the Templates pane, select Empty SharePoint Project. For the name of the project, type StateMechineWorkflow, and then click OK.
  It will open the wizard to create the project Select the site where you want to deploy the webpart. "http://najbsys2:23414/". Click  on Validate Option to validate the site details. It will show the pop-up saying that connection successful. Close the Pop-up, and select Deploy as a Farm box solution and click on Finish to create the project On the Project menu, click Add New Item.   Select State Machine Workflow template and name is 'LeaveWorkFlow' and Click on Add   On the Specify the workflow name for debugging page, leave the default name, and ensure that List Workflow is selected. Click Next   On the Select the lists you will use when debugging page, in the list labeled The library or list to associate your workflow with And Select the Tasks List for Assign the List Item.   On the Specify the conditions for how your workflow is started page, click Finish.

Step 3

After creating the application, your designer window should look like this   Drag and drop 2 state control from Toolbox | Windows Workflow v3.0 to panel State Machine Workflow Right click LeaveWorkflow1InitialState and set Name is InitialState   like that we need to change remaining 2 State like below dialog   To set an initial and completed state Right-click the state that is labeled InitialState, and then click Set as Initial State.   Right-click the state that is labeled CompletedState, and then click Set as Completed State.   To configure the InitialState activity

Step 4

Next, Double click to eventDrivenActivity1 in state InitialState   Drag a SetState activity from the Toolbox, and drop it under the onWorkflowActivated1 activity.   Right click setStateActivity1 In the Properties pane, in the TargetStateName property, click the drop-down list, and select state ‘LeaveprocessState’.   We will Get like Below dilog And Click On The LeaveWorkFlow   To configure the LeaveprocessState activity

Step 5

Right-click the ‘LeaveprocessState’ activity, and then click AddStateInitialization .   Next, drag and drop CreateTask control in Toolbox to here From the SharePoint Workflow options in the Toolbox, drag a  CreateTask activity and add it to the ‘LeaveprocessState’ drop area.   In the Properties pane, in the CorrelationToken property, type InProgressToken, and press Enter. In the Properties pane, expand the  CorrelationToken property, and in the OwnerActivityName list, select 'LeaveprocessState’   In the Properties pane, in the TaskID property, click the value, and then click the ellipsis button (...). In the Bind 'TaskID' to an activity's property dialog box, click the Bind to a new member tab.   In the Choose the type of member to create option list, click Create Field. Click OK.   In the Properties pane, click the TaskProperties property, and then click the ellipsis button (...).   In the Bind 'TaskProperties' to an activity's property dialog box, click the Bind to a new member. In the Choose the type of   member to create option list, click Create Field, and then clickOK.   Next, Double click to createTask1 in 'LeaveprocessState’   Next, copy code and paste to method generated   [csharp] private void createTask1_MethodInvoking(object sender, EventArgs e) { createTask1_TaskId1 = Guid.NewGuid(); createTask1_TaskProperties1.Title = "Request For Leave"; createTask1_TaskProperties1.AssignedTo = @"dc\Sreehari"; }[/csharp]

Step 6

Click the Design tab, and then click Workflow1 to return to the full workflow view. Right-click the 'LeaveprocessState’ activity, and then click AddEventDriven. We will Get Like below Dialog From the SharePoint Workflow options in the Toolbox, drag an onTaskChanged item to the 'LeaveprocessState’ drop Activities area. In the Properties pane, click the TaskID property, and then click the ellipsis button (...). In the Bind 'TaskID' to an activity's property dialog box, on the Bind to an existing member tab, click createTask1_TaskId1. Click OK. In the Properties pane, click the AfterProperties field, and then click the ellipsis button (...). In the Bind 'AfterProperties' to an activity's property dialog box, click the Bind to a new member tab. In the Choose the type of member to create option list, click Create Field, and then click OK. In the Properties pane, click the BeforeProperties field, and then click the ellipsis button(...). In the Bind 'BeforeProperties' to an activity's property dialog box, click the Bind to a new member tab. In the Choose the type of member to create option list, click  Create Field. Click OK. Same Like as above Process And In the Properties pane, in the CorrelationToken property, select InProgressToken. In the Properties pane, in the Invoked property, type onTaskChanged1_Invoked, and then press Enter.Add the following code [csharp]private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e) { onTaskChanged1_AfterProperties1 = onTaskChanged1.AfterProperties; onTaskChanged1_BeforeProperties1 = onTaskChanged1.BeforeProperties; }[/csharp] Click to tab LeaveWorkFlow.cs then copy code and paste here follows as [csharp] private void LeaveConditionMethod(object sender, ConditionalEventArgs e) { if (onTaskChanged1_AfterProperties1.PercentComplete == 1.0) { e.Result = true; } else { e.Result = false; } }[/csharp] Next, drag and drop IfElse control from the Window Workflow v3.0 in Toolbox to below onTaskChanged1   Click the Design tab, and then click the ifElseBranchActivity1. In the Properties pane, click the Condition property, and then select Code Condition in the drop-down list. In the Properties pane, expand the Condition property. In the drop-down list, select ‘LeaveConditionMethod’.   From the Windows Workflow 3 group in the Toolbox, drag a SetState activity to the IfElseBranchActivity1 drop area.   In the Properties pane, click TargetStateName, and then click CompletedState in the drop-down list. Total Leaveworkflow.cs Code   [csharp]using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Collections; using System.Drawing; using System.Linq; using System.Workflow.ComponentModel.Compiler; using System.Workflow.ComponentModel.Serialization; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Design; using System.Workflow.Runtime; using System.Workflow.Activities; using System.Workflow.Activities.Rules; using Microsoft.SharePoint; using Microsoft.SharePoint.Workflow; using Microsoft.SharePoint.WorkflowActions; namespace StateMechineWorkflow.LeaveWorkFlow { public sealed partial class LeaveWorkFlow : StateMachineWorkflowActivity { public LeaveWorkFlow() { InitializeComponent(); } public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); public Guid createTask1_TaskId1 = default(System.Guid); public SPWorkflowTaskProperties createTask1_TaskProperties1 = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties(); private void createTask1_MethodInvoking(object sender, EventArgs e) { createTask1_TaskId1 = Guid.NewGuid(); createTask1_TaskProperties1.Title = "Request For Leave"; createTask1_TaskProperties1.AssignedTo = @"dc\Sreehari"; } public SPWorkflowTaskProperties onTaskChanged1_AfterProperties1 = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties(); public SPWorkflowTaskProperties onTaskChanged1_BeforeProperties1 = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties(); private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e) { onTaskChanged1_AfterProperties1 = onTaskChanged1.AfterProperties; onTaskChanged1_BeforeProperties1 = onTaskChanged1.BeforeProperties; } private void LeaveConditionMethod(object sender, ConditionalEventArgs e) { if (onTaskChanged1_AfterProperties1.PercentComplete == 1.0) { e.Result = true; } else { e.Result = false; } } } }[/csharp]   Completed state machine workflow

Step 7

To deploy the project In Solution Explorer, right-click the project, and then click Deploy. After Deployed the project Open the SharePoint home page. Go to selected list Workflow Setting, Here We can check our  workflow was attached to The list.   Now We can check the Flow of the functionality , Add the NewItem into the ‘RequestLeaveForm’ List Click Save Button   After add the Item We will get the result Like below dialog , Check the ‘StateMechineWorkFlow_LeaveWorkFlow‘ Colunm Now Our Status is Inprogress   Open the Tasks list, and review the Request For Leave. Edit the Request For Leave,   in the Complete % box, type 100. Because based on our condition method ,we need update the columns, Click Save.   Here our task list status was updated   Open the Projects 'RequestLeaveForm’ and note that the List Column workflow is completed   The State Machine Workflow project in Visual Studio 2012 provides a graphical design surface in which a workflow can be built.
  • The Initial State has one activity of SetState that takes the workflow directly to the LeaveProcessState.
  • The LeaveProcessState state has a task activity that generates a new task titled "Request For Leave" and assigns the task to a specified user.
  • The LeaveProcessState state has an onTaskChanged activity that is invoked when the task changes. The inTaskChanged activity has an IfElse statement that compares the "percent complete" of the task to 1.0 (100%).
  •  the workflow continues to CompletedState. If the string does not match, the state returns to IntialState.