Loop Through Documents with SharePoint Designer 2010 Workflow

Home > Blogs > SharePoint > Loop Through Documents with SharePoint Designer 2010 Workflow

Loop Through Documents with SharePoint Designer 2010 Workflow

Like This Blog 8 Spike Xavier
Added by July 11, 2012

This is a technique I use to emulate the idea of looping through a bunch of documents in a document library in order to execute a workflow on them when they already exist in a document library. I call it the poor man's foreach!

To start, I have a normal SharePoint 2010 Team Site with the Shared Documents Library open. I have created 15 sample documents and adjusted the default view so that I can see the Type – Name – and Title columns. I will write a SharePoint Designer Workflow that sets the Title to the same value as Name with a prefix of Health Record. Going forward, I will set this to happen when a new item is created. However, in order to 'loop through' the existing documents, I will use my poor man's foreach trick.

The Library

The-Library.png

Here is my Library as stated in the scenario above.

SharePoint Designer 2010

SharePoint-Designer-2010.png

I use the Site Actions > Edit in SharePoint Designer link to open SharePoint Designer 2010 connected to the site (SPWEB).

List Workflow

List-Workflow1.png

Choose List Workflow in the Ribbon and select Shared Documents.

Name The Workflow

Name-The-Workflow1.png

I give the workflow a name. In this case it's: Title-Setter. In production, I always give a pretty solid description, but for this, I'll leave that out and hit OK.

Set Field in current item

Set-Field-In-Current-Item.png

I begin by typing set f and then hit enter. This injects Set field to value.

Click Field

Click-Field1.png

Click on field.

Choose Title

Choose-Title1.png

Choose Title.

Click Value

Click-Value1.png

Click value.

Select String Builder

Select-String-Builder.png

I select the string builder button which is the one with several dots (….)

Health Record

Health-Record1.png

Begin by typing Health Record

Add or Change Lookup

Add-Or-Change-Lookup1.png

I add a space after Health Record and then click Add or Change Lookup

Name

Name1.png

I leave the Data Source: it's default of Current Item and choose Name from the drop down menu next to Field from source: then I click OK.

Click OK

Click-OK1.png

I then click OK.

Save

Save2.png

I click the save icon in the ribbon. This places a link to the Workflow in the Navigation Window so I can adjust the ways the workflow will kick off.

Workflow Settings

Workflow-Settings.png

Click the Title-Setter link in the Navigation window.

Adjust Start Options

Adjust-Start-Options1.png

The Allow this workflow to be manually started is checked. I also check the Start workflow automatically when an item is created and Start workflow automatically when an item is changed.

Save and Publish

Save-And-Publish1.png

Choose the Save icon and when it's done saving I click the Publish Icon.

Verify Presence.

Verify-Presence.png

I switch back to the browser and click the Library Tab.

Workflow Settings

Workflow-Settings_1.png

Click the Workflow Settings icon in the right side of the ribbon.

Validate presence

Validate-presence.png

I always like to check to make sure everything is in place before I begin testing the Workflow.

Shared Documents

Shared-Documents.png

Click the Shared Documents link in the quick launch to return to the Shared Documents Library.

Select All

Select-All.png

This is how we do the poor man's foreach. If we add a document the workflow, it will do it's thing. But I need it to run on existing documents. I could go through each one which might not be so bad here because I only have 15. But if I had 150 or more, it would be more of a pain. Remember the workflow start settings included when an item is changed, so we'll just select them all , check them out and check them back in. This should kick off the workflow on each item and set the Title accordingly.

Check Out

Check-Out1.png

With the Documents all selected, I click on the Check Out icon in the Ribbon. I click OK on the alert window that says "You are about to check out the selected file(s)". (not shown)

Select all again

Select-All-Again.png

With the documents all checked out (you can see the green arrow in the icon), I click the Select all checkbox next to Type to select them all again.

Check In

Check-In1.png

With the documents all checked out, I click the Check In icon in the ribbon.

Do not Retain Checkout

I-Do-not-retain-checkout1.png

I do not retain checkout at this time so I click OK with the default setting in the Modal Window that pops up.

Validate Workflow Kickoff

Validate-Workflow-Kickoff.png

Success!!! – Well almost. The status of a couple of items is still In Progress, however we can see that the ones that have completed have set the Title to Health Record plus the Name.

This is a way get the same results as a foreach loop without code. Pretty cool. When I hit refresh, all the items are done.

Hit Refresh

I-Hit-Refresh1.png

I hit refresh after a few seconds and verified that it all ran correctly. AWESOME!

Enjoy!
Spike Xavier
SharePoint Instructor – Interface Technical Training
Phoenix, AZ

Videos You May Like

Creating Users and Managing Passwords in Microsoft Office 365

0 642 3

In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365.   For instructor-led Office 365 training classes, see our course schedulle: Spike Xavier SharePoint Instructor – Interface Technical Training Phoenix, AZ 20347: Enabling and Managing Office 365    

Using Navigation Controls in a Collaboration Site in SharePoint

0 337 1

In this SharePoint training video, I want to talk about the Navigation Controls in SharePoint. They tend to fall into two kind of different categories; one with the navigation controls in a typical Collaboration Site such as a Team Site or a Project Site. These are Sites that are based on the Team Site Template … Continue reading Using Navigation Controls in a Collaboration Site in SharePoint

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 608 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

See what people are saying...

  1. Avatar shijuraj

    That was superb thinking.. 🙂

  2. Avatar Karl

    Very good article. One quick question; can this technique be used to rename a file rather than just change the Title?
    I like to name my docs so that they reflect key metadata. I’ve tried changing the filename with workflows before but it did not work out.

    Any suggestions?

    Cheers,

    Karl

  3. Avatar SharePoint Guru

    I had more than 1000 InfoPath items published to SharePoint.
    After changing the title name from the SharePoint, the link field has broken.
    This is saving millions of time. Thanks for critical and efficient tip.

  4. Avatar Shea

    Thanks! Saved me.

  5. Avatar David

    Spike,

    Great article. Just an FYI, the "poor man's for each" also will work in a list. Just follow the same steps but when you come to the "check out" step – which you can't do in a list – switch instead to datasheet view and copy/paste a column of data back on itself. This triggers the change workflow for the whole list.

    Just did 1024 recorsd this way in like 2 minutes (workflow used an address to a Network folder in field X and copied it in correct format to be used by hyperlink field Y).

    Hope that helps anyone else out there.

    David

  6. Avatar Ramanjulu

    Hi Sir, I have followed your steps but i am unable to see "check out" option even if it is marked yes in "versioning settings". and check out option disabled for all and able to check out for single document. what could be the reason. please guide me.

  7. Avatar Shea Anderson

    Thank you. I was very surprised to learn than document file names can not be parsed in column formulas, but title fields can (we’ve never made the title field required). Thanks to your introduction to SharePoint Designer workflow creation, I’m able to populate the document titles I need and set us on a path to when this won’t bite us again.

    I might have to take some of your courses.

  8. Avatar Alan Glazier

    I would like to do something similar to this. I would like to look thru all the documents in a document set and if they meet a certain set of criteria, then modify some piece of metadata on the document set itself. In other words, if attribute a is true on 1 document and attribute b is true on another, then set the accepted flag to true on the document set. Is this even possible? Thanks, Alan

Share your thoughts...

Please fill out the comment form below to post a reply.