Looping Through Content in a SharePoint 2013 Site Workflow – Part 1 – Introduction

This entry is part 1 of 4 in the series Looping Through Content in a SharePoint 2013 Site Workflow

Site Workflows let us run logic independent of lists and libraries. By combining them with REST calls, we can traverse a Site Collection and process multiple lists and their contents by looping though them when we want. In this article, part 1 of the series, we’ll describe the problem we are trying to solve, but the solution can apply to many SharePoint scenarios.

SharePoint 2013 Site Workflow

As with most things SharePoint, setting up a workflow to loop through items across subsites seems like it should be easier. What I was trying to do seemed like a common requirement: get some data from lists in SharePoint subsites and loop through it, taking some actions on each item that meets some criteria.

I’ll warn you that this series of articles is not for the faint of heart, the new-to-workflow initiate, or the person without a lot of permissions. My solution involves reaching into Office 365’s workflow guts a bit here.

In my current case, I’m building an opportunity management solution for a sales organization. Each sales partner – these are outside companies – will have a subsite only they can access where they will enter sales opportunities as they arise. The sales folks for the host company want to be able to manage that “basket” of opportunities as well as send out emails to the partners as the opportunities move through various stages in a process. We won’t cover all of the possible permutations here as I want to focus more on the chassis of the workflow.

I need to check a specific list in a bunch of subsites for new items and send an email about each. I don’t want to create a separate List Workflow that sits in each of the lists because it would be an administrative nightmare to change the workflow down the road (we have over a dozen subsites, and the number will grow). Instead, I want to create one central SharePoint 2013 Site Workflow that finds all the subsites, finds the list in each subsite (if it exists), and iterates through the items in the list, taking different actions based on the data in each item. We’ll set this workflow to run every x minutes (probably once an hour or once a day) by adding a wait state at the end of processing.

You could accomplish this with “code” or Powershell, but that takes it out of the hands of the Site Admin who might want to change things down the road. By using a workflow, we can give them some control over things like the text of the emails without making them learn how to code to change them. As a consultant, I want to be able to leave my client with a solution they can maintain on their own as much as possible. They can still call me for the heavy lifting, but most changes should be something they can handle themselves.

On Office 365 in SharePoint Online, this feels a LOT trickier than it may have been in the past because of the complexity (nay, swamp) of authorization we must navigate. I expect that similar authentication issues may apply on premises, too, but Office 365 is more of a moving target.

In this series of articles, I’ll show you the steps I went through and let you know where I got stuck, waylaid, and confused. I believe that only one of us should have to suffer through this stuff so that everyone else can avoid it.

We’ll go through the overall concept of how the process works, as well as:

  • Setting up the workflow with the proper permissions so it can traverse content across the Site Collection
  • Initializing the workflow based on values stored in a user-maintainable list
  • Making the REST calls to discover all of the existing subsites without having to hard-wire where they are
  • Parsing the data returned from each REST call
  • Discovering whether each subsite contains the list we are looking for
  • Processing the items in each list based on a set of criteria that can change over time
  • Sending out emails appropriate for each set of criteria

This isn’t totally new ground; there are many blog posts out there that cover bits and pieces of this, and I’ll provide the links I found helpful and give credit where credit is due. I didn’t find anything that showed me how it all worked together, though.

Thanks right off the bat to Matt Bramer (@ionline247) and Fabian Williams (@fabianwilliams) for getting me up and rolling when I first start whining about this on Twitter.

This article was also published on IT Unity on 1/19/2016. Visit the post there to read additional comments.
Series NavigationLooping Through Content in a SharePoint 2013 Site Workflow – Part 2 – Setting Up App Permissions >>

Similar Posts

10 Comments

  1. Marc,
    I think it is worth saying that these are the three reasons I have found that I would use a workflow instead of JavaScript:

    1. Sending emails
    2. Implementing approval workflows, which usually includes emails
    3. Setting item level permissions ( although this can be done to some extent with JavaScript)

  2. I’d agree with you in the past (2010, 2007), I’d use workflow for changing permissions, sending emails, pausing until a date, and one other thing I can’t remember off the top of my head. Everything else was JavaScript and APIs. I can’t tell you how many workflows I’ve named: Send Email; as that was my primary use case for workflow.

    In 2013 however, sending emails can be done using JavaScript. So I only see one use case for me anymore and that’s pausing until a date. For me, I’m happy to not have to rely on workflow so much.

    Here’s a link to some code for sending emails via JS:
    http://sharepoint.stackexchange.com/a/148259/3136

    Cheers,
    Matthew

    1. @Matt: You know I’m not a big fan of workflows in general. Any idea if the JavaScript approach would work for all users, including external ones? They tend to get treated differently in all sorts of unexplained ways.

      M.

  3. That’s another use case! If “users” can modify data, you certainly can fire a workflow that does things an anonymous user cannot.

    In regards to sending an email, I’m not sure, but some of the comments say that the email initiator needs to be a SharePoint user. Seems to make sense when all things are considered.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.