Creating SharePoint Sequential Workflows with Visual Studio 2008

SharePoint Visual How To

Summary:  Learn how to create a SharePoint 2010 Sequential Workflow in Visual Studio 2010.

Applies to: Office 2007 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ben Hedges, Point8020

Overview

Visual Studio 2010 provides a template for creating a Sequential Workflows that enables developers the opportunity to build workflow solutions within SharePoint using a graphical design surface. Sequential Workflows have a pre-determined order of activities that define the workflow.

Code It

This Visual How To steps through defining a sequential workflow for a new SharePoint document library called Projects. The process includes the following steps:

  1. Creating a prerequisite document library called Projects.

  2. Adding additional fields to the Projects folder, including a Document Status choice column.

  3. Creating a Sequential Workflow with a While activity.

  4. Adding code to complete the While loop when the Document Status is set to Review Completed.

Prerequisites

This workflow requires a specific document library and three columns added to that library.

To create the Projects SharePoint document library

  1. On the Site Actions menu, click More Options.

  2. In the installed items list, click Document Library.

  3. On the right side of the screen, in the name box, type Projects and then click Create.

To support the workflow three columns are added to the Projects SharePoint document library. These will maintain the document status, the name of the person who is assigned the next task in the workflow and a column to record any review comments.

To create the document status column

  1. Browse to the Projects documents library.

  2. On the Ribbon, in the Library Tools tab group, click the Library tab.

  3. On the Ribbon, click Create Column.

  4. In the Column Name text box, type Document Status.

  5. In the Type option box, click Choice.

  6. In the Type each choice on a separate line multiline text box, type:

    • Review Needed

    • Changes Requested

    • Review Complete

    Check the spelling for these options as a text match will be used in the code.

  7. In the Default value text box, click the box, ensure that Review Needed is specified, and then click OK.

  8. On the Ribbon, click Create Column.

  9. In the Column Name text box, type Assignee.

  10. In the Type option box, click Single line of text, and then click OK.

  11. On the Ribbon, click Create Column.

  12. In the Column Name text box, type Review Comments.

  13. In the Type option box, click Multiple lines of text, and then click OK.

Creating a SharePoint 2010 Sequential Workflow Application Solution

After you create the Projects SharePoint document library and add three columns to the library, you can create the sequential workflow application solution.

To create a SharePoint 2010 sequential workflow application solution in Visual Studio 2010

  1. Start Microsoft Visual Studio 2010.

  2. On the File menu, click New, and then click Project.

  3. In the Installed Templates section, expand either Visual Basic or C#, expand SharePoint, and then click 2010.

  4. In the template pane, click Sequential Workflow.

  5. In the Name text box, type seqWorkflow.

  6. Leave other fields with their default values and then click OK.

  7. In the What local site do you want to use for debugging? list, select your site, and then click Next.

  8. On the Specify the workflow name for debugging page, leave the default name, select List Workflow, and then click Next.

  9. On the Select the lists you will use when debugging page, click The library or list to associate your workflow with drop-down, and then click Projects.

  10. Leave the other options with their default settings and click Next.

  11. On the Specify the conditions for how your workflow is started page, click Finish.

To configure the workflow

  1. With the design surface open, click the onWorkflowActivated1 activity.

  2. In the Properties window for the onWorkflowActivated activity, in the Invoked property, click the empty text box, and type onWorkflowActivated. On the keyboard, press Enter.

  3. Reopen the design surface using the tab on the main page.

  4. With the design surface on screen, open the toolbox. In the Windows Workflow 3.0 group, click the While activity, and drag it underneath the onWorkflowActivated1 activity.

    Figure 1. Creating a sequential workflow


    Creating a sequential workflow

  5. Click the While activity on the design surface. Then, in the Properties window for the activity, in the Condition row, click (none).

  6. In the drop-down selection box, click Code Condition.

  7. In the Condition row, click the expand indicator [+], and in the expanded condition row, type isWorkflowPending and then press Enter.

  8. Reopen the design surface using the tab on the main page.

  9. In the toolbox, from the SharePoint workflow group, drag OnWorkflowItemChanged and drop to the Drop an Activity Here text in the While activity.

    Figure 2. Adding an activity to a While loop


    Adding an activity to a While loop

  10. Click the OnWorkflowItemChanged activity, and then in the Properties window, in the CorrelationToken property, click the empty text box.

  11. On the drop-down selection box, click workflowToken.

  12. In the Properties window, in the Invoked property, click the empty text box, type onWorkflowItemChanged, and then press Enter.

  13. Replace the code in the Workflow1 code file with the following code.

    Imports System.ComponentModel
    Imports System.ComponentModel.Design
    Imports System.Drawing
    Imports System.Workflow.ComponentModel.Compiler
    Imports System.Workflow.ComponentModel.Serialization
    Imports System.Workflow.ComponentModel
    Imports System.Workflow.ComponentModel.Design
    Imports System.Workflow.Runtime
    Imports System.Workflow.Activities
    Imports System.Workflow.Activities.Rules
    Imports Microsoft.SharePoint.Workflow
    Imports Microsoft.SharePoint.WorkflowActions
    
    ' NOTE: When changing the namespace; please update XmlnsDefinitionAttribute 
    ' in AssemblyInfo.vb Public Class Workflow1
        Inherits SequentialWorkflowActivity
        Public Sub New()
            MyBase.New()
            InitializeComponent()
        End Sub
    
        Dim bIsWorkflowPending As Boolean = True
    
        Public workflowProperties As SPWorkflowActivationProperties = _
            New Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties
    
        Private Sub onWorkflowActivated(ByVal sender As System.Object, _
            ByVal e As System.Workflow.Activities.ExternalDataEventArgs)
            CheckStatus()
        End Sub
    
        Private Sub isWorkflowPending(ByVal sender As System.Object, _
            ByVal e As System.Workflow.Activities.ConditionalEventArgs)
            e.Result = bIsWorkflowPending
        End Sub
    
        Private Sub onWorkflowItemChanged(ByVal sender As System.Object, _
            ByVal e As System.Workflow.Activities.ExternalDataEventArgs)
            CheckStatus()
        End Sub
    
        Private Sub CheckStatus()
            If CStr(workflowProperties.Item("Document Status")) = "Review Complete" Then
                bIsWorkflowPending = False
            End If
        End Sub
    End Class
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.Drawing;
    using System.Linq;
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.ComponentModel.Serialization;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    using System.Workflow.Activities.Rules;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Workflow;
    using Microsoft.SharePoint.WorkflowActions;
    
    namespace seqWorkflow.Workflow1
    {
        public sealed partial class Workflow1 : SequentialWorkflowActivity
        {
            public Workflow1()
            {
                InitializeComponent();
            }
    
            Boolean bIsWorkflowPending = true;
    
            public Guid workflowId = default(System.Guid);
            public SPWorkflowActivationProperties workflowProperties = _
                new SPWorkflowActivationProperties();
    
            private void onWorkflowActivated(object sender, ExternalDataEventArgs e)
            {
                CheckStatus();
            }
    
            private void isWorkflowPending(object sender, ConditionalEventArgs e)
            {
                e.Result = bIsWorkflowPending;
            }
    
            private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e)
            {
                CheckStatus();
            }
            private void CheckStatus()
            { 
            if ( workflowProperties.Item["Document Status"].ToString() == "Review Complete")
            {
                bIsWorkflowPending = false;
            }
            }
        }
    }

To deploy the project

  1. In Solution Explorer, right-click the project, and then click Deploy.

  2. Open the SharePoint home page.

  3. On the Quick Launch menu, click Projects, and then click Add document.

  4. In the Upload Document dialog box, click Browse, select a document, and then click Open.

  5. In the Upload dialog box, click OK.

  6. Provide details for the uploaded document, ensure that the Document Status is set to Review Needed, and then click Save.

  7. Note that the seqworkflow is In Progress.

  8. Edit the properties for the item, change the Document Status to Review Complete, and then click Save.

  9. Note the seqworkflow is completed.

Read It

The Sequential Workflow project type in Visual Studio 2010 provides a graphical design surface on which a workflow can be constructed.

  • A While workflow activity is added to the workflow.

  • The While condition is configured as a Code Condition, and the code routine isWorkflowPending is specified.

  • The methods onWorkflowItemChanged() and onWorkflowActivated() are configured to call a CheckStatus() method that sets the bIsWorkflowPending to false if the document has the status "Review Completed".

  • Using a Code Condition, the While activity is terminated by setting a Boolean flag e.Result to True or False. True continues the While condition. False exits the While condition and allows the workflow to continue until it is completed.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/31cb63df-c640-4e32-983f-94a8bec9118b]

Length: 00:06:13

Click to grab code

Grab the code

Explore It

About the Author

Community Contributor  Ben Hedges is Senior Vice President, Research and Development at Point8020. Ben is particularly interested in modeling Talent Management, Compliance, and Learning and Development solutions on SharePoint. Ben has a wealth of experience fulfilling customer requirements by using Microsoft technologies.