Developing workflows for Microsoft Office SharePoint Server 2007 is greatly simplified in Microsoft Visual Studio 2008. With Visual Studio 2008, workflow development consists of laying out the structure of the workflow by using the workflow designer, adding code to the various event handlers associated with workflow activities, and then pressing F5 to debug the workflow.
Many SharePoint workflows use task lists to track and assign tasks to people involved in the workflows. In addition to creating task items, it is also important to understand how to inspect the state of task items as the workflow progresses. You can handle these needs by using the following SharePoint-specific activities for workflows: CreateTask and OnTaskChanged.
Creating a Task Item
By using a CreateTask activity, you can easily create tasks when you need them. To set the initial attributes associated with a task, add an event handler to the MethodInvoked event.
SPWorkflowTaskProperties spTaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
private void createTask1_MethodInvoking(object sender, EventArgs e)
createTask1.TaskId = Guid.NewGuid();
spTaskProperties = new
spTaskProperties.AssignedTo = "LITWAREINC\willisj";
spTaskProperties.DueDate = DateTime.Now.AddDays(1.0);
this.workflowProperties.Item.Name + " Review";
"Please review this document...";
createTask1.TaskProperties = spTaskProperties;
Responding to Task Item Changes
To determine when to terminate loops, decide which branch of an IfElse activity to use, or add programmatic logic to a task, you can use an OnTaskChanged activity. When an OnTaskChanged activity is encountered, you can examine the values contained in beforeProperties property collection objects, which represent the properties associated with the task before the changes occurred, or afterProperties property collection objects, which represent the properties associated with the task after the changes. To access these properties, add an event handler to the Invoked event. You can get to these properties either by binding the properties to a property or field in your project or by accessing them through the external data event arguments that are passed into the Invoked event handler.
private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
// Strongly type the event arguments.
SPTaskServiceEventArgs args = (SPTaskServiceEventArgs) e;
// Refresh the state of the task properties variable.
spTaskProperties = args.afterProperties;
// Determine whether the task is complete.
if(spTaskProperties.PercentComplete = 1.0)
bTaskNotFinished = false;
// Update the status of the item in the document library.
this.workflowProperties.Item["Workflow Note"] =
spTaskProperties.PercentComplete * 100 + "% complete...";
// Synch the due date variable with the actual due date.
dtDueDate = spTaskProperties.DueDate;
Workflow development with Visual Studio 2008 is greatly simplified to provide a more traditional development experience: Visually design the workflow structure, add code to handle events associated with workflow activities and desired functionality, and then press F5 to debug the workflow.
Designing the Workflow
Workflows consist of activities. To develop a workflow, you add activities to the workflow designer, set or configure the properties associated with each activity, and then add any code you want to respond to events associated with activities. Windows Workflow Foundation includes several activities designed to perform specific types of tasks. Office SharePoint Server 2007 also includes additional activities specific to SharePoint workflows. In Visual Studio, you add an activity to a workflow by dragging it from the Toolbox to the location you want on the designer.
You can configure some activities at design time; however, many times it is necessary to set activity properties at run time, implement logic, or perform related tasks when an activity occurs. To do this, you can attach event handlers to activity events such as the Invoked event. Just as you would generate an event handler for a control on a user control, you can generate an event handler for the Invoked event by double-clicking in the Invoked property value while viewing the events associated with the activity in the Properties window.
Looping and Branching in Sequential Workflows
You can implement looping and conditional branching logic in a workflow by using the While activity for looping and the IfElse activity for branching. Additionally, you can nest these activities to implement complicated workflow sequences. At some point, however, if the structure starts to become too complicated, consider using a State Machine workflow instead.
Debugging the Workflow
Debugging workflows by using Visual Studio 2008 is trivial compared to debugging in earlier versions. Simply press F5 in Visual Studio to start debugging the workflow. Visual Studio takes care of compiling the workflow, deploying the workflow to Office SharePoint Server, associating the workflow with your target document library or list, attaching to the w3wp.exe process, and opening an instance of Internet Explorer with the correct page (the document library or list that the workflow is associated with). In Visual Studio 2005, you had to perform all of these chores manually. To stop debugging the workflow, you might be tempted to shut down Internet Explorer and return to Visual Studio. The recommended process, however, is to simply switch to Visual Studio and click Stop Debugging (SHIFT + F5).