How to perform date calculations and construct dynamic strings in a SharePoint Designer workflow

This article demonstrates how you can perform date calculations and build dynamic strings in a SharePoint Designer 2007 workflow, save the results in variables, and then use these variables in subsequent actions in the workflow.

Scenario

When you store a document in a SharePoint document library named DepartmentDocs, you want to create a new list item in a Tasks lists named ProjectTasks. The new list item should have the name of the document in its Title and the due date of the new list item should be set to 3 days after the date the document was created.

The workflow design is shown in the following figure:

SharePoint Designer workflow design diagram

Figure 1. SharePoint Designer workflow design diagram.

You can use variables in SharePoint Designer to temporarily store the results of calculations, and then use these variables at a later stage in the workflow.

In this tutorial, you will use variables to store the results of a date calculation and a piece of text, and then use these variables afterwards to set values for a new task list item.

Before you begin, you should create a task list named ProjectTasks and a document library named DepartmentDocs in SharePoint. You should also create a new workflow in SharePoint Designer. The workflow must run on the DepartmentDocs SharePoint document library and must be set to automatically start when a new item is created.

Construct text dynamically in a SharePoint Designer workflow

To construct a piece of text (build a string) based on the name of the document that the workflow is running on:

  1. On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.
  2. On the Workflow Actions dialog box, select Core Actions from the Select a Category drop-down list box, select Build Dynamic String from the Choose an Action list, and click Add. The following action will appear on the Workflow Designer:

    Store dynamic string in Variable: variable

  3. In the text for the action, click dynamic string.
  4. On the String Builder dialog box, type Review , and then click Add Lookup.
  5. On the Define Workflow Lookup dialog box, select Current Item from the Source drop-down list box (this represents the document the workflow is running on), select Name from the Field drop-down list box, and click OK.

    The text in the String Builder dialog box should resemble:

    Review [%DepartmentDocs:Name%]

    where DepartmentDocs is the name of the SharePoint document library the workflow is running on and Name the name of the document.

  6. On the String Builder dialog box, click OK.
  7. On the Workflow Designer on the screen for Step 1, click Variable: variable, and select Create a new variable from the drop-down menu that appears.
  8. On the Edit Variable dialog box, type TaskTitle in the Name field, and click OK. The final action should look something like:

    Store Review [%DepartmentDoc... in Variable: TaskTitle

Perform date calculations in a SharePoint Designer workflow

To calculate a date based on the date the document was created:

  1. On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.
  2. On the Workflow Actions dialog box, select Core Actions from the Select a Category drop-down list box, select Add Time to Date from the Choose an Action list, and click Add. The following action will appear on the Workflow Designer:

    then Add 0 minutes to date (Output to Variable: date)

  3. In the text for the action, click 0, and type 3. Note: To subtract an amount of time, you must use a minus sign, for example -3.
  4. In the text for the action, click minutes, and select days from the drop-down menu that appears.
  5. In the text for the action, click date, and then click the formula button (second button) behind the date field.
  6. On the Define Workflow Lookup dialog box, select Current Item from the Source drop-down list box (this represents the document the workflow is running on), select Created from the Field drop-down list box (this represents the date on which the document was created), and click OK.
  7. On the Workflow Designer on the screen for Step 1, click Variable: date, and select Create a new variable from the drop-down menu that appears.
  8. On the Edit Variable dialog box, type TaskDueDate in the Name field, and click OK. The final action should resemble:

    then Add 3 days to DepartmentDocs:Created (Output to Variable: TaskDueDate)

Create a new task list item and use variables to set its field values

Variables are stored as data of the workflow and can be accessed through a source called Workflow Data.

To create a list item and make use of the data that was previously stored in the TaskTitle and TaskDueDate variables:

  1. On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.
  2. On the Workflow Actions dialog box, select List Actions from the Select a Category drop-down list box, select Create List Item from the Choose an Action list, and click Add. The following action will appear on the Workflow Designer:

    then Create item in this list (Output to Variable: create)

  3. In the text for the action, click this list.
  4. On the Create New List Item dialog box, select the ProjectTasks task list from the List drop-down list box.
  5. On the Create New List Item dialog box, select the Title(*) field, and click Modify.
  6. On the Value Assignment dialog box, click the formula button (second button) behind the To this value field.
  7. On the Define Workflow Lookup dialog box, select Workflow Data from the Source drop-down list box, select Variable: TaskTitle from the
    Field drop-down list box, and click OK.
  8. On the Value Assignment dialog box, click OK.
  9. On the Create New List Item dialog box, click Add.
  10. On the Value Assignment dialog box, select Due Date from the Set this field drop-down list box, and click the formula button (second button) behind the To this value field.
  11. On the Define Workflow Lookup dialog box, select Workflow Data from the Source drop-down list box, select Variable: TaskDueDate from the Field drop-down list box, and click OK.
  12. On the Value Assignment dialog box, click OK.

    Creating a new list item in a SharePoint Designer 2007 workflow
    Figure 2. Creating a new list item in a SharePoint Designer 2007 workflow.

  13. On the Create New List Item dialog box, click OK. The final action should look something like:

    then Create item in ProjectTasks (Output to Variable: create)

    And the actions for the entire step should now resemble:

    Store Review [%DepartmentDoc... in Variable: TaskTitle
      then Add 3 days to DepartmentDocs:Created (Output to Variable: TaskDueDate)
      then Create item in ProjectTasks (Output to Variable: create)

  14. On the Workflow Designer, click Finish to save and publish the workflow.

You should now be able to create a new document in the SharePoint document library, and when you save the document, the workflow is started and a new item is created in the task list. The new item should have the name of the document in its title and a due date of 3 days from the date on which you created the document.

Related Articles