Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#
Tip/Trick

State Machine Workflow in Sharepoint

Rate me:
Please Sign up or sign in to vote.
4.73/5 (6 votes)
8 Jan 2014CPOL2 min read 24.5K   6  
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.

Image 1

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

Image 2

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.

Image 3

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

Image 4

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

Image 5

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.

Image 6

  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).
Image 7

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

      Image 8

  1. Finally workflow looks like:

    Image 9

The code is as follows:

C#
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)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --