How to Create a SharePoint 2007 Workflow using Visual Studio 2008 Beta (Example)
A lot of people have complained that access to the Virtual Labs that I mentioned earlier in my blog is very slow and unstable. So this tutorial that I’m about to give you is based on the case in Microsoft Virtual Labs. I’ve also had a lot of requests of posting example code for creating workflows in VS2008 Beta (last one from Stephen Korow). You’re about the witness an evolution in building workflows for Microsoft, no need for command line deployment, specific configuration of workflow features, and dealing with workflow and feature xml files.
What I’m about to show you I did and tested in the following environment:
· Microsoft Windows server 2003 R2 Enterprise Edition
· Microsoft Office SharePoint Server 2007 Enterprise Edition
· MS SQL 2005 Enterprise Edition
· Visual Studio 2008 Team Suite Beta 2
· Code was done in C# only
(Note: Not that you need this exact environment. Link to the source code and word template can be found at the end of this article)
1. In your SharePoint 2007 create a Document Library (unless you already have one) which is easily achieved by choosing “Create” from the “Site Actions” menu (located in the top right corner) and then choosing” Document Library”, give it a name and click “Create”.
Now you need to create a mock contract template in Word 2007 for your Document Library
2. When in your Document Library, click the Settings dropdown and select Document Library Settings.
3. On the Customize Documents page, locate the Columns section.
4. Create a Buyer custom column:
o Click Create Column.
o Type in the column name: “Buyer”.
o Select the type Single line of text.
o For the “Require that this column contains information setting” click Yes.
o Click Ok to add the new column.
5. Add two more columns: a “Seller” column that is type Single line of text and a “Purchase Price” column that is type Currency. Both columns should be required.

6. Return to the document library home page
7. On the New dropdown in the document library, click Document.
8. A new document appears in Word. Observe that there are text boxes for the custom columns from the document library:

9. On the Office menu click Save As. Save the document as %PATH OF YOUR CHOICE%\Contract.docx. If you are prompted to confirm saving to the new Word 2007 file format, click “Ok”.
10. Insert content controls for each of the server properties:
o Place the selection in the document where you want the content control to be placed.
o Select the Insert tab on the ribbon.
o Click Quick Parts, then click Document Property, and then click Buyer.
o Repeat these steps to add content controls in the document for the Seller and the Purchase Price properties.
o For now, leave the content controls empty – do not add text to them.
11. On the Office menu, click Exit Word. When prompted to save changes to the document, click “Yes”.
12. Return to the document library in Internet Explorer, click Settings and then select Document Library Settings.
13. On the “Customize Documents” page, locate the “Content Types” section and click the Document link.
14. NB. If you can’t locate the “Content Types” section, in the “General Settings” section locate “Advanced Settings” in there switch the radio button under “Allow management of content types” to “Yes”, now when you come back to the “Customize Documents” page you should be able to locate the “Content Types” section.
15. On the “List Content Type: Document page”, click the “Advanced Settings” link.
16. Select the Upload a new document template radio button and then click Browse. Browse to select %PATH OF YOUR CHOICE%\Contract.docx and then click Ok.

17. In Internet Explorer, return to the document library.
18. Upload a new document:
o In the document library, click the “New” dropdown and then click “Document”. Word starts and loads a new document based on the contract template you created.
o Fill in the values for Title, Buyer, Seller and Purchase Price. You can fill in the values on the document properties bar (just below the ribbon) or you can fill in the content controls: the end result will be the same.
o Once all required fields have been filled in, on the Office menu, click Save.
o Save the document with a filename of your choice to the document library. If you receive a message that the document must be checked in for it to be visible to others, click OK.
o On the Office menu, click Close. When prompted to check in the document, click “Yes”.
o In the Check In dialog, click Ok to accept the version defaults. Exit Word.
o Your new document appears in the document library. Notice that the Buyer, Seller and Purchase Price columns reflect the values you entered in the Word document.

19. You may upload additional documents if you wish.
20. Close Internet Explorer.
Now comes the fun part, creating the workflow itself
21. Start Visual Studio.
22. On the File menu, click New and then click Project. The New Project dialog appears.
23. In the list of Project Types, expand Visual C#. Expand Office and then select 2007.
24. Select the SharePoint Sequential Workflow project type.
25. Name the project ContractWorkflow, specify the default location and click “OK”.

26. The New SharePoint Workflow wizard appears. Change the workflow name to Purchase Contract, use your local site where you created your Document Library (eg http://localhost/yoursite) for debugging and click “Next”.

27. Click Next to accept the default lists for debugging.
28. h. Click Finish to accept the default conditions for how a workflow is started (the default selections are Manually by users and When an item is created).
29. The new workflow appears in the designer.

Add a CreateTask Activity to the Workflow
30. If the toolbox is not displayed, then on the View menu, click Toolbox.
31. From the SharePoint Workflow tab on the control toolbox, drag a CreateTask activity and drop it onto the workflow designer between onWorkflowActivated1 and the end of the workflow (
). The new activity is named createTask1 by default.

Note: If you can't find the Create Task Activity on the toolbox (which is more than likely with VS2008 Beta 2) you need to add it manually by right clicking on the toolbar (whichever section, "General" for example) and select "Choose Items" from the menu.
The following menu selection box will show:
Tick CreateTask option and OnTaskChanged while you're at it, you'll need it later.
32. Configure the TaskID property of createTask1:
o In the Properties window, click the ellipsis for the value of the TaskID property. The Bind ‘TaskID’ to an activity’s property dialog appears.
o Type the new member name myTaskId, ensure that the Create property radio button is selected and click OK.

33. Configure the TaskProperties property of createTask1:
o In the Properties window, click the ellipsis for the value of the TaskProperties property. The Bind “TaskProperties” to an activity’s property dialog appears.
o Type the new member name “myTaskProperties”, ensure that the Create property radio button is selected and click OK.
34. In the properties window, select the MethodInvoking handler. Type MyTaskCreation and press the Enter key. The code window appears.
35. Add code to the MyTaskCreation event handler and also add the CustomFieldText function.
private void MyTaskCreation(object sender, EventArgs e)
{
try
{
myTaskID = Guid.NewGuid();
myTaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
myTaskProperties.PercentComplete = (float) 0.0;
myTaskProperties.AssignedTo = System.Threading.Thread.CurrentPrincipal.Identity.Name;
myTaskProperties.DueDate = DateTime.Now.AddDays(7);
myTaskProperties.StartDate = DateTime.Now;
myTaskProperties.Title = "Purchase Contract Workflow Task";
myTaskProperties.Description = String.Format("This is a contract for an offer to purchase real " + "estate between {0} [Buyer] and {1} [Seller]. " + "The offer amount is {2}.",
CustomFieldValue("Buyer"),
CustomFieldValue("Seller"),
CustomFieldValue("Purchase Price"));
}
catch (Exception ex)
{
//Add exception handling
throw (new Exception("Unable to initialize task.", ex));
}
}
private string CustomFieldValue(string fieldName)
{
try
{
object item = this.workflowProperties.Item[fieldName];
string s = this.workflowProperties.Item.Fields[fieldName].GetFieldValueAsText(item);
return s;
}
catch (Exception ex)
{
return String.Empty;
}
}
36. On the View menu, click Designer to return to the workflow designer.
37. In the Properties window, select the text box for the CorrelationToken property value. Type myTaskToken and press the Enter key.
38. Expand the CorrelationToken property to display the OwnerActivityName subproperty.
39. For the OwnerActivityName subproperty, select Workflow1 in the dropdown.
Add a While Activity to the Workflow
40. If the toolbox is not showing, then on View menu, click Toolbox.
41. From the Windows Workflow v3.0 tab of the toolbox, drag a While activity and drop it between createTask1 and the end of the workflow (
). The new activity is named whileActivity1 by default.
42. In the Properties window, locate the Condition property for whileActivity1.

43. Select Code Condition in the property value.
44. Expand the Condition property to display the Condition subproperty.
- 1. For the Condition subproperty, type myTaskNotCompleted and press the Enter key. The myTaskNotCompleted event handler appears in the code window.
- 2. In the Workflow1 class, add the taskCompleted variable at the class-level.
private bool taskCompleted = false;
- 3. Add code to the myTaskNotCompleted event handler that will return a result indicating whether or not the task has completed; the task is completed when its PercentComplete property is 1.0.
private void MyTaskNotCompleted(object sender, ConditionalEventArgs e)
{
e.Result = !taskCompleted;
}
Add an OnTaskChanged Activity to the Workflow
48. On the View menu, click Designer.
49. If the toolbox is not visible, then on the View menu, click Toolbox.
50. From the SharePoint Workflow tab on the toolbox, drag an OnTaskChanged activity and drop it in the center of whileActivity1. The new activity is named onTaskChanged1 by default.

51. In the Properties window, set the CorrelationToken property of onTaskChanged1 to myTaskToken.
52. Set the AfterProperties property of onTaskChanged1:
o In the Properties window, select the ellipsis for the AfterProperties property.
o On the Bind ‘AfterProperties’ to an activity’s property dialog, select the Bind to new member tab.
o Name the new member afterMyTaskPropertyChange, ensure that the Create Property radio button is selected and click OK.
53. Set the TaskId property of onTaskChanged1:
o In the Properties window, select the ellipsis for the TaskId property.
o On the Bind to an existing member tab, select the myTaskId property and click OK.
54. Double-click onTaskChanged1 in the workflow designer. The Invoked event handler for onTaskChanged1 appears in the code window.
55. Add code to onTaskChanged1_Invoked that will set the taskCompleted variable to true when the PercentComplete property of the task is 1.0.
private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
{
if (afterMyTaskPropertyChange.PercentComplete == 1.0)
{
taskCompleted = true;
}
}
56. In the Workflow1 class, set breakpoints on the MyTaskCreation and onTaskChanged1_Invoked methods.
Note: To set the breakpoints, select the first line of each method in the code windowand press the F9 key.
57. Press F5 to start debugging the project.
Note: Watch the output window as the project is built and deployed. The output window shows you many of the steps that VSTO takes for deploying the workflow solution - without VSTO, these are all steps that you would take manually. One of the SharePoint requirements for a new workflow solution is that the SharePoint worker process and IIS must be restarted; thus build/deploy times may vary.
58. Internet Explorer starts and displays the document library with the document(s) that you uploaded.
Note: You might encounter the following warning (below), if you do, click “No” do as it said and start again.
59. Select the dropdown for a document and choose Workflows.

60. In the list of workflows select Purchase Contract to start the workflow for the document.

61. Once the workflow starts, you will hit the breakpoint for MyTaskCreation. Press F5 to continue.
62. When you return to the documents library, observe that the Purchase Contract column for the document shows that the workflow is In Progress.
63. Click the In Progress link to display the workflow status.

64. Click the Purchase Contract Workflow Task link to display the task. The task properties were initialized in the MyTaskCreation method; especially note that the Description property of the task contains the data that you entered into the Word document for the buyer, seller and purchase price.
65. Click Edit Item to edit the task.

66. Change the Status to In Progress and change the % Complete value to 50% and click OK.
67. Because the task property has changed, you will hit the breakpoint at the onTaskChanged1_Invoked method.
68. Press F10 to step through the code; observe that the PercentComplete=1.0 condition is not met so the taskCompleted variable is not set to true. Press F5 to continue.
69. Complete the task:
o In the workflow status window, click the Purchase Contract Workflow Task.
o Click Edit Item, change the % Complete to 100% and click OK.
o You will hit the breakpoint at onTaskChanged1_Invoked once again.
o However, when you step through the code, you will observe that the PercentComplete=1.0 condition is met and that the taskCompleted variable is set to true this time.
o Press F5 to continue.
70. Return to the document library and observe that the Purchase Contract status now shows Complete.

71. You can test the workflow with other documents in the document library. You may also upload new documents to the library and observe how the workflow automatically starts as soon as new documents are uploaded.
72. Close Internet Explorer to stop debugging.
You can download source code to this tutorial and Contract.docx template by clicking here!!!