In this exercise we will create a custom list with 3 custom columns: FirstApprover, LastApprover, and PercentCompleted. Then we will create a workflow that creates items in the Task list for FirstApprover and LastApprover. The workflow will contain a while loop that keeps assigning a new Task item for LastApprover until he sets the custom lists PercentCompleted column to 100%. Let’s begin…

Start by creating a custom list named “myList” in your SP 2010 site. Leave the default “Title” column, and add the following 3 columns:

  • FirstApprover (Person or Group)
  • LastApprover (Person or Group)
  • PercentCompleted (Number, Number of decimal places: 0, Default value: 0, Show as percentage: checked)
  1. Open SharePoint Designer 2010 (SPD), click “Open Site”, type the URL of the site collection that you just added myList to, click the “Open” button
  2. Within the Site Objects area, click “Workflows”
  3. Click the “List Workflow” icon located near the top left of the SPD window, then select “myList” from the drop down menu that appears
  4. Enter “myWorkflow” in the “Name” textbox, and click the “OK” button
  5. At this point, SPD creates a “Step 1” by default and places the blinking horizontal orange cursor within that step, then click the “Action” icon, the select “Assign a To-do Item” from the drop down menu as shown below
  6. WfWhileLoop01
  7. Click the “a to-do item” link, then click the “Next” button on the popup window.
  8. WfWhileLoop02
  9. Enter the name, description, and then click “Finish”
  10. WfWhileLoop03
  11. Click the “these users” link, then select “Workflow Lookup for a User”, then click the “Add >>” button
  12. WfWhileLoop04
  13. Select the data source, field from source, return field as, and then click “OK” as shown below
  14. WfWhileLoop05
  15. Click the “OK” button
  16. WfWhileLoop06
  17. Click below Step 1 to position the orange cursor, then click the “Step” icon
  18. WfWhileLoop07
  19. Click within Step 2 to position the orange cursor, then click the “Action” icon, then select “Assign a To-do Item” from the drop down menu as shown below
  20. WfWhileLoop08
  21. Repeat the directions for steps 6 through 10, but enter a different name as shown below
  22. WfWhileLoop09
  23. Repeat the directions from steps 11 through 16, but select a different “Field from source” value as shown below
  24. WfWhileLoop10
  25. Repeat the direction from step 17 and 18 in order to create Step 3
  26. Click within Step 3 to position the orange cursor, then click the “Condition” icon
  27. WfWhileLoop11
  28. Select “If current item field equals value” from the condition drop down
  29. WfWhileLoop12
  30. Click on the 3 links and…
  31. WfWhileLoop13
  32. …select the values as shown below
  33. WfWhileLoop14
  34. Click within the area as shown below by “1”, then add a step by clicking the “Step” icon as show below by “2”
  35. WfWhileLoop15
  36. Click on “Step 4” and change the text to “While Statement” as shown below
  37. WfWhileLoop16
  38. Click inside the “While Statement” step to position the oragne cursor, then click the “Action” icon
  39. WfWhileLoop17
  40. Select “Assign a To-do Item” from the Action drop down list
  41. WfWhileLoop18
  42. Repeat steps 21 through 24 to set the values for “a to-do item” and “these users”, but use a different name and description as shown in the second picture below (NOTE THAT “1.00” IS USED INSTEAD OF 100% BECAUSE THAT IS NOW SHAREPOINT HANDLES NUMBER PERCENTAGES)
  43. WfWhileLoop19
  44. WfWhileLoop20
  45. Confirm that your workflow design looks the same as pictured below, then right click the “myWorkflow” tab and select “Save”. EDIT: I believe there is a typo in the screen shot below. “LastAction” should be changed to “LastApprover”.
  46. WfWhileLoop21
  47. Click “All Files” in the Site Objects area on the left of the screen, then “Workflows”, then “myWorkflow”, then right click “myWorkflow.xoml”, then “Open With, then “Notepad”
  48. WfWhileLoop22
  49. Scroll down to the end of the file and change the code in the first picture below to look like the code in the second picture below
  50. WfWhileLoop23
  51. WfWhileLoop24
  52. Select “File” from the top menu bar, then select “Save” from the drop down menu
  53. WfWhileLoop25
  54. Close the Notepad window and return to SharePoint Designer (you should be back at the “myWorkflow” folder tab)
  55. Click the “myWorkflow.xoml” link to open the “myWorkflow” tab as shown below.
  56. Ensure that the values are set as shown below, then click the “Save” button in the top left corner of the SPD window, then click the “Save” icon
  57. WfWhileLoop26A
  58. Due to the manual changes that we made in Notepad, you will be prompted, so click the “No” buttton
  59. WfWhileLoop27
  60. Click the “Publish” icon as shown below
  61. WfWhileLoop26B

EDIT: Thanks to Vincent Seckel for identifying the following omission. You must modify the web.config file of the target SharePoint application since the while activity is disabled by default. Locate the following line of code in the web.config file and change Authorized=”False” to Authorized=”True”:

<authorizedType Assembly=”System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ Namespace=”System.Workflow.*” TypeName=”WhileActivity” Authorized=”True” />

That’s it! You should now have a workflow with a while loop attached to myList. The workflow should start automatically when a new item is added to myList. Let’s confirm that it is working…

Add a new item to myList. Be sure to set the FirstApprover and LastApprover fields. Also, set the PercentCompleted value to zero.

Browse to your the Task list. You should see a task item assigned to the person that you selected for FirstApprover. Go ahead and edit the task item. Click the “Complete Task” button and the workflow will move to the next workflow step.
WfWhileLoop28

Refresh the page to see the new task item that has been assigned to the person you designated as LastApprover. Edit the task item and click the “Complete Task” button.

Refresh the page again and you will see that another task has been assigned to the person that you designated as LastApprover. This is because you did not update the myList item and set PercentCompleted column to 100%.

Go ahead and update the myList item and set PercentCompleted to 100%. Go back to the Task list and edit the task item and click the “Complete Task” button. You should not see any more tasks assigned for this workflow because you met the condition to end the workflow while loop.

If you return to myList, you should see that the “myWorkflow” column is marked as “Completed”.