In this post we will be creating a basic sequential workflow for SharePoint 2010 using Visual Studio 2010. Hopefully
you have your environment completely set up and your SharePoint sites ready to go. We will be following this guide from
Microsoft:
To begin, go into your SharePoint site and create a document library called Projects
——————————————————————————————————
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.
——————————————————————————————————
Once you have your Projects library created, add a document status column to it.
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 linemultiline 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.
Once that’s complete, you’re ready to start on developing the workflow so open up Microsoft Visual Studio 2010.
To create a SharePoint 2010 sequential workflow application solution in 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 click2010.
- 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.
You should now see a simple workflow in your Visual Studio window.
To configure the workflow
- 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 on the left-hand side of the screen. In the Windows Workflow 3.0 group, click the While activity, and drag it underneath the onWorkflowActivated1 activity.
- Click the While activity on the design surface. Then, in the Properties window for the activity, in the Conditionrow, 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, typeisWorkflowPending 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.
- Click the OnWorkflowItemChanged activity, and then in the Properties window, in the CorrelationTokenproperty, 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
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. I created aTest.txt document using notepad to use for this workflow purpose.
- 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” column is set to In Progress.
- Edit the properties for the item, change the Document Status to Review Complete, and then click Save.
- Note the “seqworkflow” column is now set to Completed.
Congratulations you’ve completed your basic Sequential Workflow!
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 aCheckStatus() 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 orFalse.True continues the While condition. False exits the While condition and allows the workflow to continue until it is completed.
No comments:
Post a Comment