Click here to Skip to main content
Click here to Skip to main content

State Machine Workflow in Sharepoint

, 8 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Create state machine workflow using Visual Studio

Introduction

This tip shows how to create custom state machine workflow using Visual Studio with the step by step snippets of it. It is useful because it covers the basics of state machine workflow and how it configures. This tip will also help to configure the properties of each activity and its methods.

When workflow starts, it assigns the task to the originator of workflow. When workflow task percentage completes set to 100% it goes to the next state (second task). You can see the second task in the task list. In the second task, if you are not setting the percentage complete 100% the workflow goes back to the first state (first task), otherwise it is in finalize state. 

Follow these Steps

  1. Create new state machine workflow project or Add state machine workflow item into existing project.
  2. It automatically creates WorkflowInitialstate state in it.

  1. In this event driven activity onworkflowactivated is automatically created. Put setStateActivity in it for the purpose of calling another state.

Add state from tools on design and bind it to setstateActivity property TargetStateName of InitialState. After setting the TargetStateName, you can see the link between them.

  1. First Task: Set stateInitializationActivity, eventdrivenactivity to the state (First Task).

In stateInitializationActivity of first task, set the createtask activity in it.

Set the properties of createtask:

  • Create its new correlationToken(FirstTaskToken) and set its owneractivityname as its state (First Task)
  • Create its TaskId (Createtask1_TaskId)and TaskProperties.

In eventdrivenactivity, set ontaskchanged activity in it.

  • Set ontaskchanged properties
    • Assign correlationToken (FirstTaskToken)
    • Assign TaskId (createtask1_TaskId)
    • Create its after properties.
    • Drag Ifelse activity on design.
    • setStateActivity on left part (if). If required set logtohistorylistactivity
    • set properties of setStateActivity targetStateName as another state(SecondTask).
    • set logtohistorylistactivity property HistoryDescription for record history.
  • Set the code condition of ifElseBranchActivity.

  1. Second Task: Perform the same activity as the first task. And set its setStateActivity targetStateName as FinalTask. Here, the only change is that set extra setStateActivity to else part (right side) for state change to any other state (Here workflow switches to firsttask).

  1. Final Task:
    • Drag one state to design.
    • Set it as final activity by choosing the "Set as complete state". See the below image.

  1. Finally workflow looks like:

The code is as follows:

public sealed partial class StateMachineWorkflow : StateMachineWorkflowActivity
    {
        public StateMachineWorkflow()
        {
            InitializeComponent();
        }
 
        public SPWorkflowActivationProperties workflowProperties = 
        				new SPWorkflowActivationProperties();
        public Guid createTask1_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties createTask1_TaskProperties = 
        	new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties onTaskChanged1_AfterProperties = 
        	new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
 
        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {
            createTask1_TaskId = Guid.NewGuid();
            createTask1_TaskProperties.Title = "First Task";
            createTask1_TaskProperties.PercentComplete = 0f;
            createTask1_TaskProperties.Description = "This is the first task 
            created after its percentage 100% done workflow goes to second state.";
            createTask1_TaskProperties.AssignedTo = workflowProperties.OriginatorUser.LoginName;
        }
 
        private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            onTaskChanged1_AfterProperties = onTaskChanged1.AfterProperties;
        }
 
        private void FirstTaskCondition(object sender, ConditionalEventArgs e)
        {
            if (onTaskChanged1_AfterProperties.PercentComplete == 1.0)
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }
 
        public Guid createTask2_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties createTask2_TaskProperties = 
        	new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties onTaskChanged2_AfterProperties = 
        	new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
 
        private void createTask2_MethodInvoking(object sender, EventArgs e)
        {
            createTask2_TaskId = Guid.NewGuid();
            createTask2_TaskProperties.Title = "Second Task";
            createTask2_TaskProperties.PercentComplete = 0f;
            createTask2_TaskProperties.Description = "This is the second task 
            	created after its percentage 100% done workflow goes to 
            		final state otherwise back to first task state.";
            createTask2_TaskProperties.AssignedTo = workflowProperties.OriginatorUser.LoginName;
        }
 
        private void onTaskChanged2_Invoked(object sender, ExternalDataEventArgs e)
        {
            onTaskChanged2_AfterProperties = onTaskChanged2.AfterProperties;
        }
 
        private void SecondTaskCondition(object sender, ConditionalEventArgs e)
        {
            if (onTaskChanged2_AfterProperties.PercentComplete == 1.0)
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

HIREN B PATEL
Software Developer
India India
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 8 Jan 2014
Article Copyright 2014 by HIREN B PATEL
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid