How To: Create a Loop Through a List of Items

Use the Create Reference and Loop steps to loop through a list of items, using one or more data fields from each item per loop.

In most instances, when you configure the Loop Step with a reference, it works as you expect, allowing you to use information about each item in each Next Item iteration of the loop. If your reference's list method does not contain an input property to retrieve a single record, and all record information in the reference is returned as a concatenated string in each loop iteration you need to follow the instructions in the Create a Loop through a List of Items using SharePoint or the How To: Loop through a list from a SQL stored procedure topics .

When you use a SmartObject method in the Loop step, the method must have an input property to load a particular record in the reference, typically an ID, autonumber or GUID, but it could also be a non-key field that acts as a filter on the method. This means that you’re passing a key or filter field as an input to the method, and that the field selected in the Create Reference step is the same as the input on the SmartObject method that you use in the Loop step. If you don’t match these or there is no input property on the method, all rows are returned and concatenated for each iteration of the loop.

The Loop step is designed to use the references to get each referenced item's values from the SmartObject when it starts each loop. When there are no more items in the reference, the loop completes.

Service object methods, such as the Get List by View method for SharePoint, do not always contain an input field. You can still configure looping in your workflow using the service object methods by creating a Loop Index and then using this index as the identifier . For more info see the How To: Create a Loop Through a List of Items using SharePoint topic

A SmartBox SmartObject Get List method automatically includes the input property for passing in the ID, but other service object methods (such as SharePoint) do not.

This example makes use of a SmartBox SmartObject Get List method.

Before you begin

This How To assumes that you are familiar with SharePoint and K2 for SharePoint.

Runtime Example

Scenario

In this example, you want to send an email to all customers that contains information about your special offers. You want to use a SharePoint list containing customer details. You start this workflow manually when a campaign starts and receive an email when all customers are notified.

The example uses a SharePoint list called My Customers. The list stores the company, contact name, and email address. The list is integrated with K2 for SharePoint and the workflow is started manually. The workflow contains three steps. A Create Reference step (creates references for each item in the list), a Loop step (that enables the workflow to loop through the referenced items), and a Send Email step that sends a customized email to a recipient.

The Loop step loops through all customers in the list and then follows the complete path to notify you when the campaign is completed.

Steps