Search This Blog

Tuesday, January 11, 2011

SharePoint Designer Workflows 2 – Developing a For Each Loop for Workflows

(For those using SharePoint 2010 please see my other post - Create a For Each Loop for Workflows Using SharePoint Designer 2010 - the technique described below will only work for SharePoint 2007)

 1. Introduction: Many times when designing workflows there is a need to use a “For Each Loop”. As you may know a For Each Loop will continue to process an action against a list of items until all items in the list have been processed. While present in all programming languages whether declared, script or object oriented, the For Each Loop is not present in SharePoint Designer workflows, or the other GUI driven workflow tools I know. This article will help you build a For Each Loop for your SharePoint Designer workflow.

2. Scenario: In our simple scenario we want our workflow to run through each of several items in a list and to update each of the items Boolean “Processed” field from No to Yes (For Each list item update Processed field to Yes).

3. Explanation: Essentially we will have two workflows, one to create and delete a new item in the “Loop” list, and the other to update the Data list. The logic for the first workflow looks like this:
a. Create an item in the Loop list

b. Pause (allowing the Loop list workflow to run)

c. Delete the item created in Loop list

The logic for the second workflow looks like this:

a. Update the first item whose “Processed” field is set to No

4. Overview: To build a For Each Loop we will need two SharePoint Lists, the first list will contain our data, and the second list will control the For Each Loop. For our example we will call the first list “Data” and the second list “Loop”. We will also need two SharePoint workflows, one, to create and delete items in the For Each Loop list, and one to update items in the Data list. To create the lists follow these steps:

a. From the SharePoint site create a custom list, call it “Data”

b. From the Data list add a custom Boolean column, call it “Processed”, set it’s default value to No and add it to the default view

c. From the Data list create several list items

d. From the SharePoint site create a second custom list, call it “Loop”

e. From the Loop list create 1 item

5. To create the workflows for these lists follow these steps:

a. From SharePoint Designer create a workflow that is associated with the Loop list, call it “For Each loop”

b. Set this workflow to start automatically when an item is created

c. Create 1 step for the workflow, call it “For Each Loop”

d. Create a condition – If Created By is not empty – this means the workflow will always run

e. Add an Update List Item activity, configure the activity as shown below:

f. Add a Pause for Duration activity, configure the activity to pause for 1 minute

g. Add a Log to History List activity, configure the log to write “For Each Loop – Ran”

h. The complete workflow configuration should appear as below:

i. From SharePoint Designer create a workflow that is associated with the Data list and call it “Process Data”

j. Set this workflow to start automatically when a item is changed

k. Create 1 step for the workflow, call it “Process Data”

l. Create a condition – If Created By is not empty

m. Create a second condition and Processed not equals No – this means the workflow will not run if the Processed equals no

n. Add a Create List Item activity, create a new item in the Loop list, title the new item “New Loop Item”, and output to variable “idLoopListItem” of the type list ID, the activity should appear as below:

o. Add a Pause for Duration activity, pause for 1 minute

p. Add a Update List Item activity, update the current item in Data and change the value of Processed to No

q. Add a Delete Item activity, and delete the item in the Loop list that has the same id as the idLoopListItem or the item you just created (by deleting the item we will keep this list from growing too large in the future). The configuration should appear as below:

r. Add a Log to History List activity, configure the log to write “Processed – Ran”

s. The complete workflow configuration should appear as below:

6. Test the Workflows: To test the workflow we only need to start the For Each Loop workflow from our item in the Loop list.

7. Error Messages: The last item created in the Loop list will have an error occurred in the workflow metadata field. This is because the last item to start cannot find any items that have not been processed. This error can be ignored; it’s part of the normal processing and will eventually be deleted by the Process Data workflow.

8. Thanks: I want to acknowledge Ricky Spears for his article “Looping through Items in a SharePoint List with SharePoint Designer Workflows”, which provided the technique that I describe in this article.


Ian Hayse said...

Thanks for the step-by-step instructions. Was about to crack open Visual Studio to make this happen.

Thank you!
Ian Hayse

Tom Molskow said...

I'm glad I could help - good luck with your project Ian!


marsiano said...

Hi - it looks like the loop stops working after 5 items...any solution?

Tom Molskow said...


Are you working with SharePoint 2007 or SharePoint 2010?



Swesh said...

Hello Tom,

You have done a great job, when I want to create a looping workflow which is almost the same scenario of yours I couldn't get more help on net, but now you have done a step by step procedure which will help a lot!! I have quoted my scenario down.. but I face a problem of, when my second list's workflow creates an item in the first list that first list's workflow is not starting automatically even though I ve choosed the option "Start wrkflw auto. when itme is added/modified"

"I want to send a summary email everyday to a set of users, so I tried to create a looping workflow using SPD, initially I couldn't create a loop later I read through some blogs and got ideas of poking the workflow. So I created two lists and two workflows one is main list and another list is poking the main. Functionality is:

If condition checks the main list for some condition and sends an email and pauses for one day duration. After the pause it creates an item in the poking list. when a new item is created 2nd workflow has started and creates a new item in the main list (as per plan)

The problem is, the second item got created in the main list but the workflow is not triggered even though the option "Start the workflow automatically when an item is created or modified" has checked!! when I edit that item then the workflow is started again. Any help would be much appreciated."

Tom Molskow said...

Hello Swesh,

Here are some suggestions that may help -

Make sure the first workflow in the first list is actually modifiying data in the second list by adding an action to log and/or send an e-mail to yourself.

Go to list Settings -> Workflow Settings -> Remove Workflow and remove the workflow that is not working. Then republish that same workflow through SharePoint Designer.

Create a new test workflow in the same list that contains the broken workflow, set it to start automatically, and then manually create a new item. If the new test workflow runs, disable (remove) the broken workflow and then run the first workflow from the first list again and see if the new test workflow starts when an item is created.

I hope that helps!


Mathias said...

Hi Tom, thanks for the article. I have the same problem as marsiano above and was wondering - have you ever gotten one of these loops to run for more than five iterations in SP 2010?
I'm trying to build a workflow using SharePoint Designer 2010 for a SharePoint Online Enterprise site that will loop through all the items in a list A and for each one create a new entry in a second list B using data from the item in list A. In order to do that, I created a workflow control list C that has an "on new item" workflow which modifies a Processed flag in A and then deletes the newly created control item. List A has an "on item updated" workflow that
- creates the new item in list B,
- then creates a new item in the control list C to continue the loop,
- then waits for a minute to let all the other items process
- and finally resets the Processed flag so the item will again be processed the next time the whole loop is started (once a week, in my case).
The whole setup is based on this post here, which is very similar to yours:

Everything works splendidly, except that the 6th item that is created in the control list C (by the workflow running against list A) simply fails to trigger its own "on new item" workflow and the loop stops. If I manually start the workflow for the sixth item in list C, the process continues but then the sixth item that should be processed in list A fails to fire the "on update" workflow.
There are a number of comments at the posting linked above that talk about the same issue, but no good answer, so I was wondering whether you have run into this and know what to do about it.


Tom Molskow said...

Hey Mathias,

Thanks for that post! I see now that this is a real problem for loop workflows done in SPD 2010. I'm going to have to build and test this in my own environment and see if I can design a solution. Check back in a week or so, if I have an answer I'll post it here and write a new blog.



Tom Molskow said...

Hey Mathias,

I have found a solution for this problem and I will be posting it soon. Please be sure to subscribe to my post so you will get the update as soon as it's out, but just in case I will also post the link here.



Anonymous said...

In step 4.p, should the value be changed to YES rather than NO? Also, I can't get the Loop item to delete.

Tom Molskow said...


When I checked your comment I realized my numbering was off, so after I have corrected that it's now actually 5.p that you are referring to.

In step 5.e you changed the value to Yes, 5.p is simply resetting everything as it was so the for each can run again, perhaps as a daily check, etc.

Are you getting any errors on the loop item? Also, make sure you values in the "Find the List Item" Field and Value entries are correct. If you can please provide me more information about your workflow, especially the portion that is not working.



Mathias said...

Thanks, that's great news - looking forward to it!


Tom Molskow said...


Hot off the presses! Here is the link to the new blog on creating a For Each Loop using SharePoint Designer 2010 Workflows:

Please give that a try and LMK if it works for you.



Mathias said...

Thanks, Tom, I'll try this out! A support person on the Office 365 community forums offered to escalate my question to a service request, so maybe that'll produce an answer as to why the straightforward approach is failing in SP 2010. I'm still curious even if there's a workaround. I'll let you know what I hear on the page for the new post. Thanks again!

shathish prabu said...

Hi Tom,

Please advice me how to proceed further. I've followed the below link to achieve my requirement but it couldn't trigger after it reaches the Step 3, even though I've update the list item.

I'm very new to the sharepoint my boss asked me to develop this, but i feel like why I've taken this job.

Please put lime light on this.


Tom Molskow said...

Hello Shathish,

If you are using MOSS 2007 please try this approach -

For SPS 2010 try this -

I hope that helps!



Tom B said...

Would this solution work with WSS3? If not is there a solution?

Tom Molskow said...

Hello Tom B,

Yes, this will also work in WSS 3.0 without any changes or problems.