How-To: For Each loop with SharePoint Designer 2010 Workflows to update multiple items

Out of the box SharePoint Designer Workflows do not have the capability of For Each loop hence the title here is misleading. However, you may run into situations where you will need to update multiple items in a list with a single action. The following steps will demonstrate how to perform updates to all items in a child list like a For Each loop. First, create a new list and name it “Parent List” and add three new items. The list should look like below:

blog_11_001

Second, create a new library and name it “Child Document Library”. Add two new columns: ParentId and Processed. List information below:

blog_11_003

Add several documents to this library and update each of the document properties:

blog_11_005

You may have figured that our goal is to loop through All items in Child Document Library and update Processed column to Yes when an item is updated in Parent List. To accomplish this we need to setup a third list which will act a loop controller. So, create a new list and name it “Loop Controller”. Create two columns in this list: Last Item Updated and ParentId.

blog_11_007

Next steps are to create workflows now that we have all the list and libraries created. We will create 3 workflows each for the lists and library we just set up and name them Parent List Workflow, Child Document Library Workflow, and Loop Controller Workflow. The names suggest which list is associated with which workflow. But before we go further along, I would like describe how it works so you get an idea when we actually develop these workflows:

1. User updates an item in Parent List
2. On list item update, the system starts Parent List Workflow which finds and updates the first unprocessed item in Child Document Library
3. On first library item update the system starts Child Document Library Workflow which creates a new list item on Loop Controller list.
4. On new item creation the system starts Loop Controller Workflow which finds and updates the Second unprocessed item in Child Document Library

At this point the looping starts. Since we updated the second item in Child Document Library which by the way creates a second list item on Loop Controller list, it triggers the Loop Controller Workflow to start again to update the third item in Child Document Library list. So on and so forth – you get the idea.

Parent List Workflow

1. Launch the site in SharePoint Designer 2010
2. Create a new List Workflow on Parent List

blog_11_009

3. Name it Parent List Workflow

blog_11_011

4. Select “Update List Item” action using Action setting on the ribbon

blog_11_013

5. Click “this list” url to open “Update List Item” dialogue.

blog_11_015

6. Set Processed field to “Yes” on Child Document Library

blog_11_017

7. Click “Yes” to the warning dialogue.

blog_11_019

8. Save the workflow and click Workflow Settings on the ribbon.

9. Check start option to start the workflow manually and when an item is changed.

blog_11_021

10. Click Publish on the ribbon to deploy the workflow.

Child Document Library Workflow

1. Create a new List Workflow on Child Document Library and name it Child Document Library Workflow

2. Select “Create List Item” action using Action setting on the ribbon

blog_11_023

3. Click “this list” url to open “Create New List Item” dialogue.

blog_11_025

4. Set Title, Last Item Updated and ParentId field.
blog_11_027

Title field Value settings below:

blog_11_029

5. Save and then set start option to start the workflow manually and when an item is changed. Finally, publish the workflow

Loop Controller Workflow

1. Create a new List Workflow on Loop Controller list and name it Loop Controller Workflow

2. Add an Update List Item action and click “this list” to bring up Update List Item dialogue

3. Set Processed field to “Yes” on Child Document Library

blog_11_031

4. Save and then set start option to start the workflow when an item is created and when an item is changed.
blog_11_033

5. Finally, publish the workflow.
To test the loop go to Parent List and click Item 1 to bring up context menu. Click Workflows and click “Parent List Workflow”. Click “Start” to start the workflow.
blog_11_035

When completed you will notice on Child Document Library Processed column has been updated and Child Document Library Workflow has been Completed.
blog_11_037

Also Loop Controller list workflow also completed adding total of 8 records for the 8 items we had in Child Document Library. Notice that the last item shows “Error Occurred” and that is because it could not find any more item to process to go further – we can ignore that error.

blog_11_039

Advertisements

19 thoughts on “How-To: For Each loop with SharePoint Designer 2010 Workflows to update multiple items

  1. Worked only for the first 5 values of the Child Document Library. 😦
    Could you please update this post to work for all the List items….

  2. As I understand the update is only done once; until processed=yes. What happens when you update the parent item again? The child folder will not be updated since the processed value=yes?

    • @Sven: You are right but to process child items again here’s what I would do 1) Disable loop controller workflow 2) Reset child items to Processed = No through Data View 3) Enable loop controller workflow.

      • Is there a way to 1) automatically disable the loop controller 2) reset the Child 3) then enable the loop controller?

  3. It seems like the workflows ignores the relationships, when I try to run them. It updates all rows in Child regardless of their parent. Do you know why this happens?

  4. What If I would only like to update only the Child list items whose parent ID is 1? This is because I don’t want all the list to be processed but only the ID that I want.

    My scenario is that I need a workflow that updates all child elements related to a single parent ID.

    Do you have any idea how can I accomplish that?

    Regards.

    • I am assuming you don’t want to hardcode the id to update in the workflow. So you can create a infopath form in SPD and check “allow workflow to be manually started”. Then from the parent list when you run the workflow it will ask you for an id to update. Hope that helps.

  5. Have you set this up where after it loops through and sets the Processed to Yes for all records you then set it to run through and set them back to No?

    • Sorry JB for not able to reply early – I have been very busy at work lately. To answer your question I have not done this. For my purpose I did this one time to update a large data set.

      • Cool Aniketa. I appreciate your help.

        1. What – “Notification System” for info sharing to anyone who has subscribed specific topics. Can not reset back to “No”
        2. When – As required
        3. Where – SharePoint List.
        4. How –
        4.a When ICT management wants to push out an iPhone information notification, the notifier will type a notice, review and then launch the workflow as designed from your notes outlined above.
        4.b Email is sent each user who has a subscription in the Child list.
        4.c Reset the “Processed” column back to “No” for future use.
        5. Final Description of Problem – This all works well, however one cannot reset the “Processed” column back to “No” without sending new emails out.
        6. Proposed solution.
        6a. Create a “Latch” list that has a column also called “Latch”.
        6b. Parent, Child and Loop workflows would each have an “IF” statement that looks to the “Latch” list.
        6C. If “Latch” is set to “Yes” then workflows would not be triggered.
        6D. If “Latch” is set to “No” then workflows would be triggered.

        Does this make sense? The only trick now would be to insure that my “If” statements in Parent, Child and Loop are able lookup the latch status. This part I am screwing up somewhere.

        Regards,
        Bartley

  6. Hi Aniketa,

    I created a similar WF as this, that need to Loop through a list, based on 2 conditions. Unfortunately this does not work (it does not find the item I’m looking for). Your aproach is for one condition, but I need for 2, as I need to update only some items, and not all at once that have the “processed” condition to “no”, for example.
    Do you have any ideea if this can be done with SPD2010?

    Thanks,
    Robert

  7. Thank very much for this info. I am not as sophisticated as others so I am confused on the Lookup Column setup in the Child Document Library. Where does it point for the lookup and how do you get the numbers you show in the example?

    Also, you say create 2 columns but here are outlined.

    Thanks very much
    John

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s