Search This Blog

Tuesday, December 23, 2014

Creating a SharePoint 2010 Sequential Workflow in Visual Studio 2010

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

  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.
create a sequential workflow
——————————————————————————————————
Once you have your Projects library created, add a document status column to it.

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 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.
    sequential workflow column
  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.
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

  1. On the File menu, click New, and then click Project.
  2. In the Installed Templates section, expand either Visual Basic or C#, expand SharePoint, and then click2010.
  3. In the template pane, click Sequential Workflow.
  4. In the Name text box, type seqWorkflow.new sequential workflow
  5. Leave other fields with their default values and then click OK.
  6. In the What local site do you want to use for debugging? list, select your site, and then click Next.sequential workflow debug
  7. On the Specify the workflow name for debugging page, leave the default name, select List Workflow, and then click Next.name sequential workflow
  8. 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.sequential workflow lists
  9. Leave the other options with their default settings and click Next.
  10. On the Specify the conditions for how your workflow is started page, click Finish.start sequential workflow

You should now see a simple workflow in your Visual Studio window.
final sequential workflow

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.sequential workflow properties
  3. Reopen the design surface using the tab on the main page.
  4. 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.sequential workflow toolbox
  5. Click the While activity on the design surface. Then, in the Properties window for the activity, in the Conditionrow, click (none).
  6. In the drop-down selection box, click Code Condition.code-condition
  7. In the Condition row, click the expand indicator [+], and in the expanded condition row, typeisWorkflowPending 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.onworkflowitemchanged
  10. Click the OnWorkflowItemChanged activity, and then in the Properties window, in the CorrelationTokenproperty, 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.workflow-token
  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

To deploy the project

  1. In Solution Explorer, right-click the project, and then click Deploy.deploy-solution
  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. I created aTest.txt document using notepad to use for this workflow purpose.test-document
  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.projects-test
  7. Note that the “seqworkflow” column is set to In Progress.
  8. Edit the properties for the item, change the Document Status to Review Complete, and then click Save.
  9. Note the “seqworkflow” column is now set to Completed.
workflow-complete
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.
  • 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