Search This Blog

Monday, October 24, 2011

Create a State Machine Workflow with SharePoint Designer 2010 - Part 1

Part 1 of this article is located here:
Create a State Machine Workflow with SharePoint Designer 2010 - Part 2


If you work with SharePoint Designer on a regular basis then you have probably built SharePoint Designer Sequential Workflows, and if you have built SharePoint Designer Sequential Workflows then you have probably been asked the question - "Can you build State Machine Workflows with SharePoint Designer?"  Well despite what you may have heard or been told the answer to that question is a definite "YES!" and this blog will show you just how that can be done.

To start with let's talk about the differences between a Sequential Workflow and a State Machine Workflow.  There are many good definitions for both types of workflows but one of the best explanantion I have read is the one provided by Stephanie Grima in her blog "[SharePoint] State Machine Workflow vs. Sequential Workflow (I have paraphrased her statements):"

 State-Machine workflows:

1.  State-machine workflows are workflows which are driven by particular state based events, for example:  
  • Approved by Submitter
  • Rejected by Compliance Officer
  • Delegated by Business Manager
  • Paused by Quality Control
2.  Until the workflow state changes, the workflow will not execute another action.
3.  The "path" of the workflow is dynamic and based what events the user executes.
4.  The workflow does not need to end, but instead can continue to execute based on changes in state.

Sequential workflows:

1. Sequential workflows are very static, they work like a flowchart, for example:  
  • Complete Step A
  • Complete Step B
  • Complete Step C
2.  Sequential workflows are useful when there is pre-determined sequence of events required to complete the task.
3.  The"path" of the workflow is rigid and based on what sequential actions are defined by the developer.

To put it succinctly, State Machine Workflows can take any path based on process state, Sequential Workflows can only take one path based on process flow.

So, now that we understand the theory let's put it into practice by creating a State Machine Workflow in SharePoint Designer (SPD) 2010.

Our Review Process Roles are as follows: 
  • Submitter 
  • Compliance Officer 
  • Business Manager 
  • Quality Control 
Our Review Process States are as follows:
  • Policy Review Started
  • Policy Review Ready for Compliance Officer Review
  • Policy Review Returned for Correction to Submitter
  • Policy Review Ready for Business Manager Review
  • Policy Review Returned for Correction to Compliance Officer
  • Policy Review Ready for Quality Control Review 
  • Policy Review Returned for Correction to Business Manager 
  • Policy Review Completed

The Compliance Officer, Business Manager or Quality Control can approve or reject the policy, and if they do they can return it back to any step in the process.  It is also true that the Submitter, Compliance Officer and Business Manager can approve the policy, and if they do they can move it ahead to any step in the process.  Here is our simple process diagram:


OK, now for the basic structure of our SPD 2010 workflow solution.  Essentially the solution consists of two workflows, the Review Process Workflow and the Review Process Task Workflow.  As their names imply the Review Process Workflow manages the main review process, and the Review Process Task Workflow task the workflow participants.  The interaction of these two workflows can be represented as shown below:

So let's start building our State Machine Workflow. Our build starts with Content Types at the Site Collection Level -  Review Process Task and Review Process Library.  Here is the structure of the Review Process Task Content Type:



The one additional modification we will need to make is to change the items in the choice list for the Task Status field.  Delete all the existing choices and simply add two choices - "Approve" and "Reject", and make sure there is no default choice configured.

And here is the structure of the Review Process Library Content Type:



Again, the one additional modification we will need to make is to change the items in the choice list for the Task Status field. Delete all the existing choices and simply add two choices - "Approve" and "Reject", and make sure there is no default choice configured.

Notice that both content types make use of three custom content coulmns Rerview Process Roles, Review Process States, and Workflow ID. Here is the structure of Review Process Roles choice column:
  • Submitter
  • Compliance Officer
  • Business Manager
  • Quality Control
And here is the structure of the Review Process States choice column:
  • Policy Review Started
  • Policy Review Ready for Compliance Officer Review
  • Policy Review Returned for Correction to Submitter
  • Policy Review Ready for Business Manager Review
  • Policy Review Returned for Correction to Compliance Officer
  • Policy Review Ready for Quality Control Review
  • Policy Review Returned for Correction to Business Manager
  • Policy Review Completed
The Workflow ID column is just a simple number column set to have 0 decimal places.
Next we need to create the Review Process Library as a document library and the Review Process Task as a task list. Once these are created replace the default Content Types with our custom Content Types. This will make the default Content Type for the Review Process Library the Review Process Library Content Type and the default Content Type for the Review Process Task list the Review Process Task Content Type. 

OK, now for the good part, we need to make two reuseable workflows - Review Process Workflow and Review Task Workflow.  As you may have guessed, the Review Process Workflow will use the Review Process Library Content Type, and the Review Task Workflow will use the Review Process Task Content Type.  (To see how to create reuseable workflows in SHarePoint Designer click here).

The psuedo logic for the Review Process Workflow is as follows:
  • Submitter
    • State = Policy Review Started
    • Or If Approve and Sate = Policy Review Ready for Compliance Officer Review
    • Or If Approve and State = Policy Review Ready for Business Manager Review
    • Or If Approve and State = Policy Review Ready for Quality Control Review
    • Then Create New Task
  • Compliance Officer
    • If Reject and Sate = Policy Review Returned for Correction to Submitter
    • Or If Approve and Sate = Policy Review Ready for Business Manager Review
    • Or If Approve and Sate = Policy Review Ready for Quality Control Review
    • Then Create New Task
  • Business Manager
    • If Reject and Sate = Policy Review Returned for Correction to Submitter
    • Or If Reject and Sate = Policy Review Returned for Correction to Compliance Officer
    • If Approve and Sate = Policy Review Ready for Quality Control Review
    • Then Create New Task
  • Quality Control
    • State = Policy Review Completed
    • Or If Reject and Sate = Policy Review Returned for Correction to Submitter
    • Or If Reject and Sate = Policy Review Returned for Correction to Compliance Officer
    • Or If Reject and Sate = Policy Review Returned for Correction to Business Manager
    • Then Create New Task
The psuedo logic for the Review Task Workflow is as follows:
  • If State has Changed then Update Review Process Library
  • Else Log "No Change"
From the psuedo logic we can see that the Review Process Workflow contains the logic to capture all the possible states and then creates the appropriate task for the appropriate role based on that state.  On the other hand the Review Task Workflow simply updates the Review Process Library with the current state based on the outcome of the task.

Now let's build our State-Machine Workflow!!! I'll cover the steps for construction in my next article - Create a State Machine Workflow with SharePoint Designer 2010 - Part 2 (Coming Soon!!!).



References:





No comments: