SPD Workflow - Test if value exists in any item in SharePoint list - create new item if not found - SharePoint Development & Administration + InfoPath

Wednesday, August 19, 2009

SPD Workflow - Test if value exists in any item in SharePoint list - create new item if not found

SharePoint Designer Workflow: How to test if a specific value exists in any item in a list without the workflow stopping due to a "List item not found" error if no items exist with the required value.

When trying to find a list item using the "Update List Item" action or similar in a SharePoint Designer workflow by matching a field which is not always unique (eg. the "Title" column/field), the workflow will stop if the list item is not found with a "List item not found" error. To prevent this, a looping workflow can be used to build a delimited string containing the value from the field being tested, for all items in the list.
Instead of trying to test if a specific value exists in any item in a list by testing the required field directly, the workflow can compare the string containing all possible values using a much larger range of useful conditions including "contains", "does not contain", "contains (ignoring case)", which can be used to test if the required value exists. If the value is not found, the workflow will continue as normal without causing an error as only a simple string compare had taken place.

This can be quite useful if you want the workflow to do one action if an item is a seperate list containing a specific value is found, and another action if not found (without stopping due to workflow error). This can be used to test if the is an item already exists in the list containing a specific value, and to use this item if found. If the item does not exist, then one is created (example/scenario below).

SharePoint Designer does not provide standard functionality to create a looping workflow, but it can be achieved using two separate lists with on-change workflows that trigger each other. A "flag" is set (Boolean) to determine if an item has been processed by this instance of the workflow, and used to find the next item to be processed. Once updated, the on-change workflow can either pause for a duration to allow the remaining items to be processed before resetting the flag, or a similar workflow can loop back through to reset each item.

The article Service Pack 2 prevents an on-change workflow from starting itself demonstrates how to create looping workflows using SharePoint Designer, but also provides important information about the difference in Workflow functionality between Service Pack 1 and Service Pack 2 for MOSS 2007. If you had looping worklfows functioning prior to upgrading to service pack 2, they may not work if the loop was being controlled from a single on-change workflow for this reason.

Example: Update an item if one exists with a specific title, or create/add a new item to the list if an item is not found with the required title field.


  1. Using a looping workflow, append the Title from each item in the required list in a separate list item to a string field separate to the items being processed.
  2. In the main workflow, build a dynamic string containing the required title value. Note: The "Build a dynamic string" action allows fields of different data types (Dates, People, Numbers) to be converted into a string and concatenated if required.
  3. Using the "Compare any data source" workflow condition, test if the required value is, contained in the string of all possible titles.
  4. If the title does not exist in the string, the workflow can then create a new item. If the title does exist in the string, then the workflow will be able to find the list item to be updated and won't stop due to the "List item not found error"

Related Posts:

SharePoint Looping Workflow - How to: Loop through and process all items in a list
"The OOB workflow actions provided by SharePoint don't allow a loop to be configured explicitly. Instead, a number of workflows can work together to create a looping affect. Another limitation of Workflows in SharePoint using SharePoint Designer (SPD) is the inability to simply test if the user who initiated the workflow is a member of a SharePoint group, or to test if the value of a field in the item which initiated the workflow is equal to the value of a specific field from any item in a separate list."


How to wait for a change in any list, wait for multiple field changes in the current item (SharePoint Workflow)

This article describes techniques which can be used when developing workflows in SharePoint using SharePoint Designer. They allow a workflow to wait for a field change in an item in another list, or to wait for multiple field changes in the current item before continuing.

Looping Timer Workflows
To have a looping timer workflow (eg. send a daily notification/reminder until a specific condition is met), the workflow must start when an item in the list is changed, and when it is created depending on the scenario.

How to configure a workflow to start at specific time, then loop / repeat periodically (Daily, Weekly)The following method can be used to start a workflow at a specific time, then repeat periodically (each day). Periodic Workflow Processing: (send daily email notification or reminder)A useful implementation of this technique allows you to configure workflows to run as background tasks at a specific time instead of being triggered by a user manually, or after creating or modifying a list item.


SharePoint Workflow Errors: Descriptions, resolutions and WorkaroundsThe following table lists some of the common errors that can occur in a Workflow developed using SharePoint Designer. A short description of some of the likely causes for each error are also provided.

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment