Dela via


Walkthrough: Creating and Debugging a SharePoint Workflow Solution

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • SharePoint workflow

Microsoft Office version

  • SharePoint Server 2007

For more information, see Features Available by Application and Project Type.

This walkthrough demonstrates how to create a basic sequential workflow template. The workflow checks a property of a document library to determine whether a document has been reviewed. If the document has been reviewed, the workflow finishes.

This walkthrough illustrates the following tasks:

  • Creating a SharePoint 2007 sequential workflow project.

  • Creating a workflow schedule.

  • Handling activity events.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, select Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

For more information about how to create SharePoint workflow templates using Visual Studio Tools for Office, see SharePoint Workflow Solutions.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).

  • Microsoft SharePoint Server 2007.

  • A SharePoint Server Web site that has the title Document Center and has the URL named https://localhost/Docs. If you installed Microsoft Office SharePoint Server 2007 on a single computer as a stand-alone installation, this Web site is created automatically when you complete the SharePoint Products and Technologies Configuration Wizard.

  • Microsoft Office Word 2007 or Microsoft Office Word 2003.

Visual Studio Tools for Office is installed by default with the listed versions of Visual Studio. To check whether it is installed, see Installing Visual Studio Tools for Office.

Adding Properties to the SharePoint Document Library

To track the review status of documents in the Documents library, workflow participants set the Document Status, Assignee, and Review Comments properties of the document. Define these properties in the document library.

To add properties to the SharePoint document library

  1. Open the default SharePoint Server 2007 Web site in Internet Explorer.

    Note

    By default, the SharePoint site is at https://localhost. If you map https://localhost to a non-SharePoint Web site, you will not be able to test the workflow template using the steps in this walkthrough.

  2. Click the Document Center tab.

  3. In the navigation pane, click Documents.

  4. In the Documents page, click Settings, and then click Document Library Settings.

  5. In the Customize Documents page, under Columns, click Create Column.

  6. Name the column Document Status, select Choice (menu to choose from), and specify the following three choices, and then click OK:

    • Review Needed

    • Review Complete

    • Changes Requested

  7. Create two more columns and name them Assignee and Review Comments. Format the Assignee column as a single line of text, and the Review Comments column as multiple lines of text.

Enabling Documents to be Edited without Requiring a Check Out

It is easier to test the workflow template when you can edit the documents without having to check them out.

To enable documents to be edited without checking them out

  1. In the Customize Documents page, under General Settings, click Versioning Settings.

  2. For Require documents to be checked out before they can be edited, select No, and then click OK.

  3. Close Internet Explorer.

Creating a SharePoint Sequential Workflow Project

A sequential workflow is a procession of steps that execute in order until the last activity finishes.

To create a SharePoint sequential workflow project

  1. Start Visual Studio.

  2. Open the New Project dialog box, expand the Office node under the language you want to use, and then select the 2007 node.

  3. In the Templates pane, select SharePoint 2007 Sequential Workflow.

  4. In the Name box, type MySharePointWorkflow. If your IDE is set to use Visual C# development settings or General development settings, enter a location and solution name.

  5. Click OK.

    The New Office SharePoint Workflow wizard appears.

  6. In the Specify the workflow name and site for debugging page, click Next to accept the default workflow name and site.

  7. In the Select the lists you will use when debugging page, click Next to accept the default document library, task list, and history list and to automatically associate your workflow template with the document library.

  8. In the You can specify the conditions for how your workflow is started page, click Finish to accept the default selections.

Creating a Workflow Schedule

Workflow templates contain one or more workflow schedules, which contain activities that represent actions to perform. Use the workflow designer to arrange activities on a schedule. To monitor the review status of a document in the Documents list, add two activities: HandleExternalEventActivity and Microsoft.SharePoint.WorkflowActions.OnWorkflowItemChanged.

To create a workflow schedule

  1. In Solution Explorer, double-click Workflow1.cs or Workflow1.vb to open the workflow schedule in the designer.

  2. In the designer, click the OnWorkflowActivated1 activity to select it.

  3. In the Properties window, type onWorkflowActivated next to the Invoked property, and then press ENTER.

    The Code Editor opens, and an event handler method named onWorkflowActivated is added to the Workflow1 code file.

  4. Re-open the workflow schedule in the designer.

  5. On the View menu, click Toolbox.

  6. From the Windows Workflow v3.0 tab of the Toolbox, drag a While activity under the onWorkflowActivated1 activity.

  7. Click the WhileActivity1 activity to select it.

  8. In the Properties window, set Condition to Code Condition.

  9. Expand the Condition property, and type isWorkflowPending next to the child Condition property, and then press ENTER.

    The Code Editor opens, and a method named isWorkflowPending is added to the Workflow1 code file.

  10. Re-open the workflow schedule in the designer.

  11. From the SharePoint Workflow tab of the Toolbox, drag an OnWorkflowItemChanged activity inside the whileActivity1 activity.

  12. Click the onWorkflowItemChanged1 activity to select it.

  13. In the Properties window, set properties as shown in the following table.

    Property

    Value

    CorrelationToken

    workflowToken

    Invoked

    onWorkflowItemChanged

Handling Activity Events

The last step is to check the status of the document from each activity. If the document has been reviewed, the workflow is finished.

To handle activity events

  1. In Solution Explorer, right-click Workflow1.cs or Workflow1.vb, and then click View Code.

  2. Add the following field at the top of the Workflow1 class. You will use this field in an activity to determine whether the workflow is finished.

    Dim workflowPending As Boolean = True
    
    Boolean workflowPending = true;
    
  3. Add the following method to the Workflow1 class. This method checks the value of the Document Status property of the Documents list to determine whether the document has been reviewed. If the Document Status property is set to Review Complete, then the checkStatus method sets the workflowPending field to false to indicate that the workflow is ready to finish.

    Private Sub checkStatus()
        If CStr(workflowProperties.Item("Document Status")) = "Review Complete" Then
            workflowPending = False
        End If
    End Sub 
    
    private void checkStatus()
    {
        if ((string)workflowProperties.Item["Document Status"] == "Review Complete")
        workflowPending = false;
    }
    
  4. Add the following code to the onWorkflowActivated and onWorkflowItemChanged methods to call the checkStatus method. When the workflow starts, the onWorkflowActivated method calls the checkStatus method to determine whether the document has already been reviewed. If it has not been reviewed, the workflow continues. When the document is saved, the onWorkflowItemChanged method calls the checkStatus method again to determine whether the document has been reviewed. While the workflowPending field is set to true, the workflow continues to run.

    Private Sub onWorkflowActivated(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.ExternalDataEventArgs)
        checkStatus()
    End Sub
    
    Private Sub onWorkflowItemChanged(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.ExternalDataEventArgs)
        checkStatus()
    End Sub
    
    private void onWorkflowActivated(object sender, ExternalDataEventArgs e)
    {
        checkStatus();
    }
    
    private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e)
    {
        checkStatus();
    }
    
  5. Add the following code to the isWorkflowPending method to check the status of the workflowPending property. Each time the document is saved, the whileActivity1 activity calls the isWorkflowPending method. This method examines the Result property of the ConditionalEventArgs object to determine whether the WhileActivity1 activity should continue or finish. If the property is set to true, the activity continues. Otherwise, the activity finishes and the workflow finishes.

    Private Sub isWorkflowPending(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.ConditionalEventArgs)
        e.Result = workflowPending
    End Sub
    
    private void isWorkflowPending(object sender, ConditionalEventArgs e)
    {
        e.Result = workflowPending;
    }
    

Testing the SharePoint Workflow Template

When you start the debugger, Visual Studio Tools for Office deploys the workflow template to SharePoint Server 2007 and associates the workflow template with the Documents list. To test the workflow, start an instance of the workflow template from a document in the Documents list.

To test the SharePoint workflow template

  1. In Solution Explorer, right-click Workflow1.cs or Workflow1.vb, and then click View Code.

  2. Set a breakpoint next to the onWorkflowActivated method.

  3. Press F5.

    The Documents page of the default SharePoint site opens.

  4. In the Documents page, click New to create a new document.

  5. Save the document to the default location.

    This adds the document to the Documents list and starts the workflow.

  6. In Visual Studio, verify that the debugger stops at the breakpoint next to the onWorkflowActivated method.

  7. Press F5 to continue execution.

  8. Close the document.

  9. Return to the Documents page of the default SharePoint Web site.

  10. In the Documents page, verify that the value underneath the MySharePointWorkflow column is set to In Progress. This indicates that the workflow is in progress and that the document is awaiting review.

  11. In the Documents page, point to the document, click the down arrow, and then click Edit Properties.

  12. Set Document Status to Review Complete and then click OK.

  13. Return to the Documents page of the default SharePoint Web site.

  14. In the Documents page, verify that the value underneath the MySharePointWorkflow column is set to Completed. This indicates that workflow is finished and that the document has been reviewed.

Next Steps

You can learn more about how to create workflow templates from these topics:

See Also

Tasks

How to: Include Helper Files When you Debug a SharePoint Solution

SharePoint Sequential Workflow Sample

Concepts

SharePoint Workflow Solutions

Debugging SharePoint Workflow Solutions

Reference

SharePoint Workflow Project Templates