While loops in SharePoint Designer Workflows
When implementing processes, we’re often faced with creating workflows that require something like “while not approved, ask document to be edited and re-approved”. This is difficult to do with SharePoint Designer workflows as it requires looping.
For a while now I’ve been using our Custom Workflow Activity Pack to create loops in SharePoint designer. This simulates a state workflow and is described here: Stateful Workflow Paper. This approach works, but is cumbersome and involves a number of moving parts. A “While Workflow” custom action has now been added that greatly simplifies doing this. This custom action takes four arguments:
- A field in the document library or list with which the workflow is associated.
- A condition to test (e.g. “equal to”, “greater than”).
- A value to test against (either a constant or a lookup to another value in the list, a workflow variable or value from another list)
- The workflow to call to create the loop. This workflow will be repeatedly called while the expression in the first three arguments returns false.
In this example, a document library has two fields “Verified” (Yes/No) and “Verify Comments” (Single line text). Two workflows need to be created:
- A workflow (e.g. named “Verify Document”) that contains the task that is to be repeated
- Another (e.g. named “While Not Verified”) workflow that repeatedly calls the first workflow until the task is completed successfully.
The first workflow, “Verify Document” in this example, simply creates a task requesting the user to verify the document. After the task is completed the “Verified” field is updated in the document library:
The second workflow uses the “While Workflow” custom action to create the loop. This will repeatedly call the “Verify Document” workflow until the document is verified.
You can download the latest version of the Custom Activity Pack here. A SharePoint solution file (WSP) is now provided to automate installation. If you already have a previous version installed, follow the instructions in the documentation to uninstall the previous version first.
Update: See here how while loops are now directly supported in SharePoint 2013: https://nickgrattan.wordpress.com/2012/07/22/while-loops-in-sharepoint-designer-2013-workflows/
Update: 10-Nov-2013. Source code and WSP now available in GitHub: https://github.com/NickGrattan/NGCCustomActivities.git