Blog

SharePoint Workflow Looping

SharePoint Workflow Looping

On a recent project, I was given an interesting challenge by a customer using SharePoint. Basically the desire was to have a recursive "For loop" function in SharePoint using nothing but native tools (i.e. workflows). Googling the subject, I found very little helpful information, but I was able to come up with a workable solution of my own and thought it was worth blogging about.

The desired scenario was to have a bunch of records in a list where a user could perform a process on all of them (i.e. Approve drafts waiting for approval). In order to start this, the user would just use an action button on a form that would approve all of the records. This is easy to do in most programming languages, but without for loops in SharePoint, I had to rely on an "On Modify" workflow to solve my problem.

The crux of this solution is twofold. Firstly, each record needs to have a "State" variable assigned to it. This can be as simple as a text column with the value "Unapproved", "Approving", or "Approved". The second piece of the puzzle is the use of "On Modify" workflows. For the conditions of these workflows, the test and modifications need to be done on a lookup item from list, NOT current item. The architecture is best explained in the steps below:

  1. Action workflow updates 1 record state column from "Unapproved" to "Approving"
  2. On Modify workflow is triggered by previous step. It checks if there are any records in the "Approving" state (if not it terminates). If so it checks if there are any records still in the "Unapproved" state. If so, it goes to step 3, if not it skips to step 5.
  3. The On Modify workflow updates the next "Unapproved" record to "Approving"
  4. This triggers another "On Modify" workflow, returning to step 2
  5. Once there are no records left in "Unapproved" but records in the "Approving" state, the workflow updates the state column in the first "Approving" record it finds to be "Approved"
  6. This change triggers another On Modify workflow that then checks if there are more records in the "Approving" state, and if so returns to step 5.
  7. Once there are no "Approving" records left, all of the records should have been successfully moved from "Unapproved" to "Approved".

This example then creates a For Loop in SharePoint workflows, requiring the user to only press 1 "Approve All" button!

Workflow Diagram

Learn more about DMC's SharePoint workflow services.

Comments

Kannan
# Kannan
Hi myself kannan, i have one doubt in sequential workflow. i have a scenario in SharePoint that, when uploading a document in a document library it goes for approval. i have written Visual studio sequential workflow for this. Here the Workflow approvers are not fixed its continuously going. how can looping this ????????? any help would be appreciate.
finesse
# finesse
Trying to implement something similar while trying to prevent a sort of endless recusive loop.
Can I email you?
Sofia Khatoon
# Sofia Khatoon
Excellent solution. Thanks for posting this.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Related Blog Posts