3. Visual Studio 2010
Visual Studio 2010 is a development
tool for developing various types of applications and services. It
provides development project types for different SharePoint
customizations and project
templates for SharePoint 2010 workflows. Visual Studio 2010 uses the
Windows Workflows Foundation (WF) designer to define a workflow’s
activities, the order in which to execute them, and the conditions in
which to perform them. In the following sections, you explore some of
the important aspects of workflow development within Visual Studio 2010.
3.1. Visual Studio Workflow Project Types
A developer can
create sequential or state machine workflows in Visual Studio 2010.
Alternatively, it is possible to import a SharePoint Designer–based
reusable workflow to create a workflow solution. Visual Studio 2010
provides a project template, shown in Figure 6, for all of these types of workflow development activities.
Figure 6. SharePoint 2010 project templates in Visual Studio 2010
The figure contains the
following list of SharePoint 2010 project templates available in Visual
Studio 2010 .
Sequential Workflow In a sequential
workflow project, the activities happen in a specific sequence from
start to finish. You can branch these activities with the help of
conditions. This type of workflow is ideal for creating approval or
feedback collection scenarios, where activities happen in sequence:
first there is a request for feedback or approval, then the feedback or
approval is sent, and so forth.
State Machine Workflow
A state machine workflow represents a set of states, transitions, and
actions. The steps in a state machine workflow execute asynchronously,
and they are triggered by actions and states. One state is assigned as
the start state, and then, based on an event, a transition is made to
another state. The state machine can have a final state that determines
the end of the workflow.
Import Reusable Workflow This project type is used for importing a SharePoint Designer 2010–based reusable workflow for further development, by adding code or activities that are not supported by SharePoint Designer.
3.2. Standard Workflow Activities in Visual Studio
The activities available for use while developing a workflow appear in the Toolbox on the left side of the screen, as shown in Figure 7.
A developer can drag these activities onto the design surface to define
the steps in a workflow. The properties of each activity can then be
set in the Properties window that appears in the lower-right corner. The
Base Activity Library of Windows Workflow Foundation provides a group
of fundamental activities, and SharePoint 2010 also provides a set of
activities designed expressly for creating workflows.
Figure 7. Visual Studio 2010 standard workflow activities
The following workflow activities are commonly used when designing SharePoint 2010 workflows.
OnWorkflowActivated Provides a standard starting point for a workflow. This activity
can accept information supplied by a SharePoint administrator by using
the Association form when the workflow is associated with a document
library, list, content type, or site. It can also accept information
supplied by the Initiation form when the workflow is started. Every
workflow must begin with this activity.
CreateTask Creates a task assigned to a particular user in a task list. This activity also has a SendEmailNotification
property that, when set to True, automatically sends an e-mail message
to the person for whom this task was created.
OnTaskChanged Accepts information from the Task Completion form, which can be used in the rules and other actions in the workflow.
CompleteTask Marks a task as completed.
DeleteTask Removes a task from a task list.
Accepts information from the Modification form, which can then be used
to change how this instance of the workflow behaves. If the workflow’s
creator chooses not to include any instances of this activity in the
workflow, that workflow cannot be modified while it is running. This
activity is responsible for reassigning a task to another use, adding
more participants to a running workflow, and so on.
SendEmail Sends an e-mail message to a specified person or group of people.
Writes information about the workflow’s execution to a history list.
The information in this list is used to let users see where a workflow
is in its execution, look at the workflow’s history after it is
completed, and more. To allow this kind of monitoring, the workflow’s
author must write information to a history list at appropriate points in
the workflow’s execution. Because it provides its own mechanism for
tracking workflows, SharePoint 2010 doesn’t support the Windows Workflow
Foundation’s standard tracking service.
3.3. Creating Workflows in Visual Studio
The creation of a simple workflow in Visual Studio 2010 begins with choosing the template—a sequential or state machine workflow. (Figure 8
shows an example of a workflow created using the Sequence template.)
After selecting the appropriate template, you specify a site that will
be used for deploying, debugging, and testing the workflow during development.
You then specify the workflow association type as either a Site or List
workflow. Finally, you specify the workflow history and the task list
to use for testing the workflow. At this point, the graphical workflow
designer is displayed; this is where you define the workflow logic.
A simple workflow in Visual Studio 2010 begins with an OnWorkflowActivated activity and then uses a CreateTask activity to assign a task to participants in the workflow. The standard WhileActivity
can be used to wait until the users complete the tasks and provide
necessary data through the task form, which is shown as part of task
activity. An OnTaskChanged
activity executes within the WhileActivity, extracting whatever
information the user has entered on that form. Based on the information
collected, the workflow can use the CodeActivity activity to perform any
complex logic and update data on the item or document with which this
workflow is associated.
SharePoint 2010 provides all
activities that are available in Visual Studio 2010 under SharePoint
Workflow. These activities are designed to perform typical activities
that a user would perform in the SharePoint 2010 environment. The
business logic a workflow implements is determined by the developer of
the workflow. In fact, a developer authoring a workflow is free to
create and use custom activities and is not restricted to activities
provided by SharePoint 2010 or Windows Workflow Foundation.
Figure 8. A sequential workflow in Visual Studio 2010
Apart from designing the workflow’s logic, the workflow developer is also responsible for creating
the forms that will interact with the users. There are four types of
forms a workflow developer can create: Association, Initiation, Task,
and Modification forms. As the workflow develops, you must do some work
to pass information between the workflow and its .ASPX or InfoPath
forms. The Microsoft.Windows.SharePoint.Workflow
namespace exposes an object model for developers. By using the types in
this namespace, the creator of a workflow can pass information from a
form to the workflow and vice versa.
After the workflow and its
forms have been created, you use a SharePoint Feature to package the
workflow for deployment. An Element.xml file, part of the feature
definition, is used for binding the forms with the workflow. When the
feature definition is created, this feature, along with other features,
pages, and so on, are packaged into a solution (WSP) for deployment.
Visual Studio 2010 project templates are capable of creating the solution packages.
As the workflow
developer, you can debug the workflows in Visual Studio 2010 simply by
pressing the F5 key, which deploys the workflow, activates the workflow
on the chosen site, and associates the workflow to the list or site
chosen earlier. The developer can complete the necessary workflow tasks
and see that the control comes back to the Workflow Designer for the
next task on the workflow. This way you can validate that each activity
on the workflow is executing as expected and yields the correct results.
After validation, the developer delivers the solution package to be
used on a SharePoint 2010 farm.
A SharePoint farm
administrator must then install and deploy the solution to all Web
applications where the workflow is to be used. The site collection
administrators can activate the workflow feature on their site
collection. The new workflow will now be visible to the site
administrators as a workflow template that can be associated with a
document library, list, content type, or site.
4. Workflow Modeling Tools Comparison
Use Table 2 to help you determine which workflow development tool is best for creating your workflows.
Table 2. Workflow Modeling Tools Comparison
|FUNCTIONALITY||SHAREPOINT DESIGNER 2010||VISUAL STUDIO 2010|
|Can be scoped to a site collection||Yes||Yes|
|Workflows are accessible in client applications (other than the browser)||Yes||Yes|
|Can use ASP.Net forms as workflow’s forms||Yes||Yes|
|Can use InfoPath forms as workflow’s forms||Yes||Yes|
|One-click publishing available for workflows||Yes||Yes|
|Can be used for modifying predefined workflows||Yes||Yes|
|Can be used to create workflows with serial activities||Yes||Yes|
|Can be used to create workflows with parallel activities||Yes||Yes|
|Can be used to create sequential workflows||Yes||Yes|
|Workflows can be created using only actions that are approved by SharePoint administrators||Yes||No|
|Can use Visio 2010 to create workflow logic||Yes||No|
|Workflows can be deployed remotely from workflow authoring environment||Yes||No|
|Can be used to create state machine workflows||No||Yes|
|Need to write code||No||Yes|
|Additional activities (other than the ones provided by SharePoint 2010) can be used in creating workflows||No||Yes|
|Workflow can be modified while it is running||No||Yes|
|Can be used to create workflow that is made available across the farm||No||Yes|
|Can be used to create custom workflow activities||No||Yes|
|Can be used to create workflows that interact with external systems||No||Yes|