Creating SharePoint 2010 State Machine Workflows in Visual Studio 2010
Microsoft Visual Studio 2010 provides a State Machine Workflow template that enables you to build workflow solutions for Microsoft SharePoint 2010 by using a graphical design surface. Unlike sequential workflows, which transition from activity to activity, state machine workflows transition from state to state.
This SharePoint Visual How To describes the following steps for creating and deploying a state machine workflow to a SharePoint 2010 site:
- Creating a prerequisite document library named Projects.
- Adding state activities for stateInProgress, stateReview, and stateFinished.
- Configuring state initialization and events for each state.
- Configuring code that determines state transitions.
Prerequisites
This workflow requires a specific document library named Projects.
To create the Projects 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.
Creating a Workflow
Begin by creating a SharePoint State Machine Workflow project in Visual Studio 2010.
To create a SharePoint State Machine Workflow project in Visual Studio 2010
- In Visual Studio, 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 State Machine Workflow.
- In the Name box, type stateWorkflow. Leave the default values in the other fields, and click OK.
- In the SharePoint Customization Wizard, 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, and ensure that List Workflow is selected. Click Next.
- On the Select the lists you will use when debugging page, in the list labeled The library or list to associate your workflow with, select 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.
The design surface appears with the Workflow1IntitalState activity.
To add states to the workflow
- From the Windows Workflow 3 group in the Toolbox, drag three State activities onto the design surface.
- Click the WorkFlow1InitialState activity, and in the Properties pane, in the Name property, rename this state to InitialState.
- Click the stateActivity1 activity, and in the Properties pane, in the Name property, rename this state tostateInProgress.
- Click the stateActivity2 activity, and in the Properties pane, in the Name property, rename this state tostateReview.
- Click the stateActivity3 activity, and in the Properties pane, in the Name property, rename this state tostateFinished.Figure 1 shows the three states that were added to the design surface.
To set an initial and completed state
- Right-click the state that is labeled InitialState, and then click Set as Initial State.
- Right-click the state that is labeled stateFinished, and then click Set as Completed State.
To configure the InitialState activity
- In the InitialState activity, double-click eventDrivenActivity1. A new view appears.
- Drag a SetState activity from the Toolbox, and drop it under the onWorkflowActivated1 activity.
- In the Properties pane, in the TargetStateName property, click the drop-down list, and selectstateInProgress.
- Click Workflow1 on the design surface to return to the full workflow view.
To configure the stateInProgress activity
- Right-click the stateInProgress activity, and then click AddStateInitialization.
- In the Properties pane, rename the stateInitializationActivity1 to stateInProgressInitialization.
- From the SharePoint Workflow options in the Toolbox, drag a CreateTask activity and add it to thestateInProgressInitialization drop area.
- In the Properties pane, in the CorrelationToken property, type InProgressToken, and press Enter.
- In the Properties pane, expand the CorrelationToken property, and in the OwnerActivityName list, select stateInProgress.
- In the Properties pane, in the TaskID property, click the value, and then click the ellipsis button (...).
- In the Bind 'TaskID' to an activity's property dialog box, click the Bind to a new member tab.
- In the Choose the type of member to create option list, click Create Field. Click OK.
- In the Properties pane, click the TaskProperties property, and then click the ellipsis button (...).
- In the Bind 'TaskProperties' to an activity's property dialog box, click the Bind to a new membertab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, in the MethodInvoking property, type createTask1_MethodInvoking, and press Enter.
- Add the following code.
- Click the Design tab, and then click Workflow1 to return to the full workflow view.
- Right-click the stateInProgress activity, and then click AddEventDriven.
- From the SharePoint Workflow options in the Toolbox, drag an onTaskChanged item to thestateInProgress drop area.
- In the Properties pane, click the TaskID property, and then click the ellipsis button (...).
- In the Bind 'TaskID' to an activity's property dialog box, on the Bind to an existing member tab, click createTask1_TaskId1. Click OK.
- In the Properties pane, click the AfterProperties field, and then click the ellipsis button (...).
- In the Bind 'AfterProperties' to an activity's property dialog box, click the Bind to a new membertab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, click the BeforeProperties field, and then click the ellipsis button (...).
- In the Bind 'BeforeProperties' to an activity's property dialog box, click the Bind to a new membertab.
- In the Choose the type of member to create option list, click Create Field. Click OK.
- In the Properties pane, in the CorrelationToken property, select InProgressToken.
- In the Properties pane, in the Invoked property, type onTaskChanged1_Invoked, and then press Enter.
- Add the following code:
- Click the Design tab. From the Toolbox, in the Windows Workflow 3 group, drag an IfElse activity below the OnTaskedChange1 activity.
- In the Workflow1 code view, add the following code.
- Click the Design tab, and then click the ifElseBranchActivity1. In the Properties pane, click theCondition property, and then select Code Condition in the drop-down list.
- In the Properties pane, expand the Condition property. In the drop-down list, select ReadyForReview.
- From the Windows Workflow 3 group in the Toolbox, drag a SetState activity to theIfElseBranchActivity1 drop area.
- In the Properties pane, click TargetStateName, and then click stateReview in the drop-down list.
- On the Design surface, click Workflow1 to move back to the full workflow view.
To configure the stateReview activity
- Right-click the stateReview activity, and then click AddStateInitialization.
- From the SharePoint Workflow options in the Toolbox, drag a CreateTask activity to thestateInitializationActivity1 drop area.
- In the Properties pane, click the (Name) property and rename it to createReviewTask.
- In the Properties pane, in the CorrelationToken property, type ReviewStateToken, and press Enter.
- In the Properties pane, expand the CorrelationToken property, and in the OwnerActivityName list, select stateReview.
- In the Properties pane, click the TaskID, and then click the ellipsis button (...).
- In the Bind 'TaskID' to an activity's property dialog box, click the Bind to a new member tab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, click the TaskProperties property, and then click the ellipsis button (...).
- In the Bind 'TaskProperties' to an activity's property dialog box, click the Bind to a new membertab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, in the MethodInvoking handler, type createReviewTask_MethodInvoking, and then press Enter.
- Add the following code.
- Click the Design surface, and then click Workflow1 to move back to the full workflow view.
- Right-click the stateReview activity, and then click AddEventDriven.
- From the SharePoint Workflow options in the Toolbox, drag an onTaskChanged item to thestateReview drop area.
- In the Properties pane, set the Correlation Token to ReviewStateToken.
- In the Properties pane, click the TaskID property, and then click the ellipsis button (...).
- In the Bind 'TaskID' to an activity's property dialog box, on the Bind to an existing member tab, click createReviewTask_TaskId1, and then click OK.
- In the Properties pane, click the AfterProperties field, and then click the ellipsis button (...).
- In the Bind 'AfterProperties' to an activity's property dialog box, click the Bind to a new membertab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, click the BeforeProperties field, and then click the ellipsis button (...).
- In the Bind ' BeforeProperties ' to an activity's property dialog box, click the Bind to a new member tab.
- In the Choose the type of member to create option list, click Create Field, and then click OK.
- In the Properties pane, in the Invoked handler, type onTaskChanged2_Invoked, and then press Enter.
- Add the following code.
- Click the Design tab. From the Toolbox, in the Windows Workflow 3 group, drag an IfElse activity below the OnTaskedChange2 activity.
- Open the Workflow1 code view, and add the following code.
- Click the Design tab, and then click the ifElseBranchActivity3. In the Properties pane, click theCondition property, and select Code Condition in the drop-down list.
- In the Properties pane, expand the Condition property.
- In the expanded condition field, select ReviewFinished in the drop-down list.
- From the Windows Workflow 3 group in the Toolbox, drag an IfElse activity and add it below theifElseActivity3 activity in the Drop Activities here area.This creates a nested If/Else statement.
- Add the following code in the Workflow1 code view.
- Click the Design tab, and then click the ifElseBranchActivity5. In the Properties pane, click theCondition property, and select Code Condition in the drop-down list.
- In the Properties pane, expand the Condition property. In the expanded condition field, selectDocApproved in the drop-down list.
- From the Toolbox, drag a SetState activity under the ifElseBranchActivity5 activity.
- In the Properties pane, click the TargetStateName property, and then select stateFinished.
- From the Toolbox, drag a SetState activity under the ifElseBranchActivity6 activity.
- In the Properties pane, click the TargetStateName property, and then select stateInProgress.Figure 4 shows the finished state machine workflow.
Deploying the Project
Finally, deploy the project and test your workflow.
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. Click OK in the Upload dialog box.
- Open the Tasks library, and review the Finish Document task.
- Edit the Finish Document task, and in the Complete % box, type 100. Click Save.Note the Review Document task.
- Edit the Review Document task, set the status to Completed, and set Complete % to 100. In the Description, type Approved, and then click Save.
- Open the Projects document library and note that the document workflow is complete.
The State Machine Workflow project in Visual Studio 2010 provides a graphical design surface in which a workflow can be built.
- The Initial State has one activity of SetState that takes the workflow directly to the stateInProgressstate.
- The stateInProgress state has a task activity that generates a new task titled "Finish Document" and assigns the task to a specified user.
- The stateInProgress state has an onTaskChanged activity that is invoked when the task changes. TheinTaskChanged activity has an IfElse statement that compares the "percent complete" of the task to 1.0 (100%).
- If the condition is true, the workflow transitions to the next state (stateReview). If it is false, the workflow remains at this state.
- In the stateReview state, a task is generated with a title of "Review Document" as part of the state initialization, and again this task is assigned to a specified user.
- The stateReview state is invoked when the task changes, and an IfElse statement compares the "percent complete" of this task with 100 percent. As before, when the task is 100 percent complete, the workflow continues.
- When the task is 100 percent complete, a nested IfElse statement is reached within the stateReviewstate. The code compares the text that appears in the task description to the string "<DIV>Approved</DIV>".
- If the string matches, the stage is considered complete and the workflow continues to StateFinished. If the string does not match, the state returns to stateInProgress.
Comments
Post a Comment