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.
This Visual How To steps through defining a sequential workflow for a new SharePoint document library called Projects. The process includes the following steps:
Creating a prerequisite document library called Projects.
Adding additional fields to the Projects folder, including a Document Status choice column.
Creating a Sequential Workflow with a While activity.
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
On the Site Actions menu, click More Options.
In the installed items list, click Document Library.
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
Browse to the Projects documents library.
On the Ribbon, in the Library Tools tab group, click the Library tab.
On the Ribbon, click Create Column.
In the Column Name text box, type Document Status.
In the Type option box, click Choice.
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.
In the Default value text box, click the box, ensure that Review Needed is specified, and then click OK.
On the Ribbon, click Create Column.
In the Column Name text box, type Assignee.
In the Type option box, click Single line of text, and then click OK.
On the Ribbon, click Create Column.
In the Column Name text box, type Review Comments.
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
Start Microsoft Visual Studio 2010.
On the File menu, click New, and then click Project.
In the Installed Templates section, expand either Visual Basic or C#, expand SharePoint, and then click 2010.
In the template pane, click Sequential Workflow.
In the Name text box, type seqWorkflow.
Leave other fields with their default values and then click OK.
In the What local site do you want to use for debugging? list, select your site, and then click Next.
On the Specify the workflow name for debugging page, leave the default name, select List Workflow, and then click Next.
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.
Leave the other options with their default settings and click Next.
On the Specify the conditions for how your workflow is started page, click Finish.
With the design surface open, click the onWorkflowActivated1 activity.
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.
Reopen the design surface using the tab on the main page.
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
Click the While activity on the design surface. Then, in the Properties window for the activity, in the Condition row, click (none).
In the drop-down selection box, click Code Condition.
In the Condition row, click the expand indicator [+], and in the expanded condition row, type isWorkflowPending and then press Enter.
Reopen the design surface using the tab on the main page.
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
Click the OnWorkflowItemChanged activity, and then in the Properties window, in the CorrelationToken property, click the empty text box.
On the drop-down selection box, click workflowToken.
In the Properties window, in the Invoked property, click the empty text box, type onWorkflowItemChanged, and then press Enter.
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
In Solution Explorer, right-click the project, and then click Deploy.
Open the SharePoint home page.
On the Quick Launch menu, click Projects, and then click Add document.
In the Upload Document dialog box, click Browse, select a document, and then click Open.
In the Upload dialog box, click OK.
Provide details for the uploaded document, ensure that the Document Status is set to Review Needed, and then click Save.
Note that the seqworkflow is In Progress.
Edit the properties for the item, change the Document Status to Review Complete, and then click Save.
Note the seqworkflow is completed.
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.
|
Watch the video
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/31cb63df-c640-4e32-983f-94a8bec9118b]
Length: 00:06:13
Grab the code
About the Author
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. |