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:

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 alsocreate 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:On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.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:
Storedynamic string inVariable: variableIn the text for the action, click dynamic string.On the String Builder dialog box, type Review , and then click Add Lookup.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.On the String Builder dialog box, click OK.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.On the Edit Variable dialog box, type TaskTitle in the Name field, and click OK. The final action should look something like:
StoreReview [%DepartmentDoc… inVariable: TaskTitlePerform date calculations in a SharePoint Designer workflow
To calculate a date based on the date the document was created:On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.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 Add0minutes todate (Output toVariable: date)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.In the text for the action, click minutes, and select days from the drop-down menu that appears.In the text for the action, click date, and then click the formula button (second button) behind the date field.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.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.On the Edit Variable dialog box, type TaskDueDate in the Name field, and click OK. The final action should resemble:
then Add3days toDepartmentDocs:Created (Output toVariable: 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:On the Workflow Designer on the screen for Step 1, click Actions, and select More Actions.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 inthis list (Output toVariable: create)In the text for the action, click this list.On the Create New List Item dialog box, select the ProjectTasks task list from the List drop-down list box.On the Create New List Item dialog box, select the Title(*) field, and click Modify.On the Value Assignment dialog box, click the formula button (second button) behind the To this value field.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.On the Value Assignment dialog box, click OK.On the Create New List Item dialog box, click Add.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.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.On the Value Assignment dialog box, click OK.

Figure 2. Creating a new list item in a SharePoint Designer 2007 workflow.On the Create New List Item dialog box, click OK. The final action should look something like:
then Create item inProjectTasks (Output toVariable: create)
And the actions for the entire step should now resemble:
StoreReview [%DepartmentDoc… inVariable: TaskTitle
  then Add3days toDepartmentDocs:Created (Output toVariable: TaskDueDate)
  then Create item inProjectTasks (Output toVariable: create)
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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s