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
In this step you create a list with three custom columns and integrate the list with K2.
- Create a list called My Customers on a SharePoint site.
- Create three columns in the list called Company (Single line of text), Name (Single line of text) and Email (Single line of text).
- Click the new item link and add three customers to the list. Use the same email address, such as administrator@denallix.com, for all three customers.
- On the List tab, click the Applicationbutton to integrate the list with K2. You may need to switch to SharePoint Classic mode. For more information see SharePoint Online User Interface Change.
- Select the Create SmartForms check box on the Create K2 Application page.
- Check the Create Workflow option and When the following events occur, selecting the Workflow is manually started option.
- Click OK to complete the integration and create a K2 application for the My Customers list.
In this step you start building the workflow by adding and configuring the Create Reference step.
- After integrating the My Customers list with K2, the K2 Workflow Designer appears.
If you are not taken to the K2 Workflow Designer automatically, select the My Customers workflow on the K2 artifacts page and click Edit to launch the workflow designer.
- From the Toolbox click the Basic category, then drag the Create Reference step onto the canvas.
- Select the step and click the expand / collapse toggle to expand the Configuration Panel. You can also double click the step to expand the panel. Select the Create Reference tab to configure the reference. From the SmartObject menu, browse and select the My Customer SmartObject. From the Method menu, select the Get List Items option. From the Identifier menu, select the ID option.
- Configure the Fill Reference section. Click Create to create the My Customers 1 reference.
- Click the expand / collapse toggle to expand the Configuration Panel. Notice the My Customers 1 reference.
In this step you add the Loop step to the workflow and specify what reference to use to loop through to send email to your customers.
- From the Toolbox click the Logic category, then drag the Loop step onto the canvas. Connect the step to the Create Reference step.
- Select the step and click the expand / collapse toggle to expand the Configuration Panel. You can also double click the step to expand the panel. In the List section, select the My Customer 1 reference from the menu.
- After you choose a reference, the new list item reference is automatically created for you. This new reference allows you to access the current customer and their details. To view the item reference, expand the Options section.
In this step you add the Send Email step and configure the email that your customers receive.
- From the Toolbox click the Basic category, then drag the Send Email step onto the canvas. Connect the step to the Next Item path of the Loop step.
- Select the step and click the expand / collapse toggle to expand the Configuration Panel. You can also double click the step to expand the panel. Select the Email tab, click the toggle button, and delete Originator from the To field.
- Configure the To address by dragging the Email property from the My Customers 1 Item reference into the To address. This adds the customer email address. You can find the reference in the Context Browser > References section, and then expand the My Customers 1 Item reference to see the various properties of the reference.
- Configure the Subject and Body sections. In the Subject field type Special Deals for the Month. In the Body field type Hi, Please visit our website to view this month's specials, and then drag the Name property from the My Customers 1 Item reference into the notification body.
- The Loop step creates two paths, namely Next Item and Complete. You need to connect the Next Item path back to the Loop step so the step can successfully loop. This action ensures that your workflow loops through the entire list.
In this step you add the Send Email step and configure the email that you receive as confirmation that the loop completed.
- From the Toolbox click the Basic category, then drag the Send Email step onto the canvas. Connect the step to the Complete path of the Loop step.
- Select the step and click the expand / collapse toggle to expand the Configuration Panel. You can also double click the step to expand the panel. Select the Email tab and ensure the Originator is configured in the To address field.
- Configure the Subject and Body sections. In the Subject field type Monthly Specials sent to My Customers. In the Body field, type Loop Completed.
In this step you add the End step to the workflow.
- From the Toolbox, click the Logic category and drag the End step onto the canvas. Connect the step to the Send Email step.
- Your workflow configuration is done and ready to be deployed.
In this step you deploy your workflow.
- Select File and click the Deploy option.
- Deployment successful.
- Select File and click Close to close the workflow.
In this step you start the workflow manually to start the loop and send the mails to your customers.
- Using K2 Management manually start an instance of the My Customers workflow.
- The workflow loops through the list and sends an email to each of the names on the list.
In this example, the list contains three customer (Kyle, Sandra and Frank).