How To: Create a Loop Through a List of Items using SharePoint
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. You only need to follow the instructions in this topic 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.
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.
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. For more information, see the How To: Create a Loop Through a List of Items topic.
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.
This How To shows you how to create the Loop Index and configure the loop in a workflow. This example is meant for illustration purposes only and is not a best practice.
Before you begin
This How To assumes that you are familiar with SharePoint and K2 for SharePoint.
Runtime Example
Scenario
You want to use a list of customers in a workflow to send a notification email of available specials.
This scenario uses a SharePoint list called Contact Details. The list stores a list of names and email addresses. You use the SharePoint method called Get List Item By View to iterate through each customer's details to send an email. 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.
You use the Loop Index reference as the identifier that calls each item in the list.
Steps
In this step you configure the list to show each list item's ID. This is necessary because you're going to use the Get list item by View method, which would not include the ID if you don't configure it to show.
- Open the Contact Details SharePoint list. Select the List tab and click the Modify View option.
- On the Edit View page, check the Display box for the ID column. This is not shown by default. Click OK.
- Although the ID column is part of the list, it does not display on the view by default. By checking the check box, the ID column now shows.
In this step you create the Loop Index to use the ID you exposed in step 1.
- Using the K2 Designer select the Mailing List workflow and click Edit.
- Select the Loop step and expand / collapse the configuration panel. Expand Options, locate the Index section, and click Create to create an index reference.
- The Loop Index reference stores the current loop index. At runtime the selected loop index is used to loop through the list of customers and send each one a personalized email.
- Click the expand / collapse toggle to expand the Configuration Panel. Notice the new Loop Index variable.
In this step you configure the Send Email step to use the loop index variable in the address, subject and body of the mail.
- 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 to add the customer email address. From the Context Browser, select SmartObjects. Browse to the Contact Details SmartObject and select the Get List Items By View method.
- From the Get List Items By View method, select the Email property and drag it into the To field.
- The Smartfield Composer shows. Select ID from the Name drop down menu.
- From the Context Browser, select Fields and then drag the Loop Index variable into the ID property field. In the View Title field type the view name as All Items.
All Items is the name of the default SharePoint list view.
- Click X to close the Smartfield Composer.
- Configure the Subject and Body sections. In the Subject field type Monthly Newsletter. In the Body field type Hi, We have exciting news this month....
- From the Context Browser, select SmartObjects. Browse to the Contact Details SmartObject and select the Get List Items By View method.
- From the Get List Items By View method, select the Name property and drag it into the body of the email.
- The Smartfield Composer shows. Select ID from the Name drop down menu.
- From the Context Browser, select Fields and then drag the Loop Index variable into the ID property field. In the View Title field type the view name as All Items.
All Items is the name of the default SharePoint list view.
- Click X to close the Smartfield Composer. The Send Email step configured as shown below.
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 manually start the workflow to begin looping through the customer list and sending emails.
- Using K2 Management, manually start an instance of the Mailing List workflow.
- The workflow loops through the list and sends an email to each of the names on the list. Using the Loop Index to filter on the ID and ensures that the customized name property shows correctly in the individual emails.
In this example, the list contains three customer (Bob, Codi and Dennis).
- Email received by the Originator shows the loop completed successfully.