How to Loop Through a SharePoint 2013 List Skipping Items that Do Not Exist

This is a quick and dirty site workflow I created to update a bunch of items in a list. Because this is a quick and dirty solution, I did not want to make a REST call and use a dictionary to store the list. As a result I simply loop through a predefined number of items in the list, check if the item exists, and if it does update the list item.

This solution does not use any web service calls to accomplish its goal.

In SharePoint Designer 2013, I first created a site workflow. I then created two variables called, “index” and “ExistingID”.

index_variable_pic      existing_variable_pic

  1. Set the “index” to 0.
  2. Create a loop that will loop a specified number of times.
  3. Set the “ExistingID” equal to the list item where the ID is equal to the index.
    1. If the Item with the ID of index exists, it will return the ID number of the document. If it does not exists, it will return 0.
      Example:
      set_variable_eq_others
      Here is what the “Others:ID” assignment looks like
      others_pic
  4. Create an If statement that checks if the ExistingID variable is not equal to 0.
  5. Within the If statement block, perform the action that you would like on the item, because it does exist.
  6. Outside the if statement block, perform a calculation on the “index” variable that adds 1 to it. Output the result back into the “index” variable.

Here is an example of what the completed workflow would look like:

workflow_steps_pic

In this example I had a document library called “Others” where I loop over a parameter called “Number of Other Docs” that is specified upon initiation of the workflow.

A better way to accomplish this procedure, where you would receive a list of only the items you want, would be to make a REST call to the list, and put the ID’s into a dictionary. Loop over the dictionary results, and perform on operation on the list items whose ID’s are in the dictionary.

Leave a Reply

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