PortiBlog

Looping SharePoint 2010 Workflows

4 maart 2016

Imagine that, a workflow in SharePoint 2010 that can loop. However without doing something special this is not possible (we will keep it simple). I know it’s an oldy, but you will never know in what situation you are and you cannot use the latest technologies available.

Note! With SharePoint 2013 workflows this is possible out of the box, however in this example your company is running on SharePoint 2010.

Before we will explain this, first a (normal) situation. Your company is completely into SharePoint and you work on the sales department with lots of opportunities. These opportunities are registered in a document library (let’s call this library Opportunities) with an opportunity number. Every document (for example research or background information) are registered with this unique number.

Also you have a separate library (let’s call this one Email) to store every email which is applicable to the opportunity. These emails are also registered with the opportunity number.

Then you get the call, one of the opportunities is accepted by the customer. Now you get to create the order for the opportunity. This order will be created in a separate library (let’s call this one Orders). An order will also get an order number. To get a complete overview of the order you would want all the previously made documents in one place. You want all the documents from the opportunity in the order library and that all documents have the unique order number.

Note! For the usage of the workflow also the opportunity number is registered in a separate column.

FirstImageNormally you would have to manually move all the required documents and set the unique order number. However, the number of documents from the opportunity can exceed like 20 documents and 30 emails. So you must collect documents from several places and move them to one location and set metadata.

This can be time consuming and good news, there is an easier way. By using workflows you can automate this process and you can quickly go to the new opportunity or order.

To set this up there is a little trick needed because a workflow cannot initiate itself (a SharePoint Designer 2010 restriction and a way to save you from a never ending loop). The trick is to use an extra library (let’s call this MoveLibrary).

 

Note! All Libraries must have the same metadata fields for this situation to work correct.

You will need 2 workflows, below a quick schematic of the two workflows. The steps are listed below the schematic.

FirstImage2

A small workflow (wf1:) on the extra library MoveLibrary composed of four rules:

  1. The workflow must be set that it will run when a new item is created
  2. Pause for 5 minutes (this is necessary to make sure all the metadata is set)
  3. Copy the current item to the library Orders
  4. Delete the current item from the library MoveLibrary

The second workflow (wf2:) will run on the destination library Orders and is composed by the following rules:

  1. The workflow must be set that it will run when a new item is created
  2. Pause for 5 minutes (this is necessary for the looping and to make sure all the metadata is set)
  3. A check (if CurrentItem:OppertunityID equals Opportunities:OpportunityID
  4. Update the item in Opportunities with the order number (for example in the field OrderID)
  5. Then copy the item from Opportunities to the MoveLibrary
  6. Then delete the item from Opportunities
  7. Use an Else to catch/throw a log if there are no more items found.
  8. For the emails, repeat steps 3 through 7 for that library and after publishing the workflows you are all set.

See below for a screenshot from SharePoint Designer where workflow 2 is visible in more detail. The If and Else part is needed for every location you want to check based on a common identifier.

FirstImage3

Let’s recap this, what is the behavior now?

When you create a new document in the library Orders the workflow starts with a pause of 5 minutes, then it checks if it can find any document (it will only find the first result) that has the same OpportunityID. Then it updates the first found item in the library Opportunities with the order number, copies the item from the library Opportunities to the MoveLibrary and deletes it from the Opportunities library. With that it is the end of the first workflow.

In the MoveLibrary the item is created and the workflow start with a pause of 5 minutes, again waiting for the metadata. Then it copies the current item to the final destination, the library Orders, and then deletes the current item. With that it is the end of this workflow and now for the loooooop.

There is a new item created in the Orders library through the workflow of the MoveLibrary and with the creation of a new item the workflow starts again. Also starting with a pause of 5 minutes to wait for all the metadata to be set correctly. This process will run until there are no more items found in the Opportunities of Email library.

There you have it, a full blown loop until it can’t find anymore items, thanks to the assistance of the MoveLibrary. Now you only have to make the order and the workflows do the rest so you have more time to concentrate on getting more orders.

Submit a comment