Step by Step Tutorial : Creating workflows for Sharepoint 2010: Introduction (step 1/15)

image_thumb_thumb

SharePoint 2010 workflow tutorial

 

  1. Step 1.
  2. Step 2.
  3. Step 3.
  4. Step 4.
  5. Step 5.
  6. Step 6.
  7. Step 7.
  8. Step 8.
  9. Step 9
  10. Step 10.
  11. Step 11.
  12. Step 12
  13. Step 13.
  14. Step 14.
  15. Step 15.

 

SharePoint 2007 workflow tutorial

 

  1. Step 1.
  2. Step 2.
  3. Step 3.
  4. Step 4.
  5. Step 5.
  6. Step 6.
  7. Step 7.
  8. Step 8.
  9. Step 9.
  10. Step 10.
  11. Step 11.
  12. Step 12.
  13. Step 13.
  14. Step 14.
  15. Step 15.
  16. Step 16.
  17. Step 17.

 

Introduction

One of the greatest innovation in the previous version of Sharepoint (MOSS 2007 and WSS v3) was the integration with Windows Workflow Foundation, a core component of the .Net Framework 3.0.

The last version of Sharepoint, Microsoft Sharepoint Server 2010Microsoft Foundation 2010, is based on the .Net framework 3.5; the workflow part of .Net 3.5 has not been slightly modified, the .Net concepts are the same.

I’m actually adapting to Sharepoint 2010 a very popular workflow tutorial I created several years ago for Sharepoint 2007.

If you want a general overview of Workflow Foundation, you can read this  article from David Chappel.

You can also take a look at the article I published in SharePoint Pro magazine.

In this long series of articles, I will provide a step by step approach to building workflows in Sharepoint with both Microsoft Visual Studio 2010, Microsoft Sharepoint Designer 2010 and Visio 2010.

Scope

This post will just cover the basic part : creating the project, deploying the workflow and explaining some basic concepts.Since it is a step by step tutorial, the code we are going to create here will be upgraded in other posts.

You can download the final solution + the site template here.

Submitting an expense report : the scenario

The workflow we are going to create step by step is an application that allows users to submit their expense reports; as soon as their expense report will be submitted, a workflow will be activated; this workflow will:

  • Generate a unique identifier (a guid) for this expense report
  • Check if the report must be submitted to approval : if the amount is less than 1000, the expense report will automatically be approved and the process is completed (status will be “autoapproved”)
  • Find the user’s manager
  • Create tasks for the manager to approve or reject the expense report
  • Update the expense report status (“approved or “rejected”).

I use the Information Worker virtual machine provided for free by Microsoft.

Before starting, you need a site (don’t create it from scratch, but create it from the following site template) with the following lists:

A list named “Expense Reports” with the following columns:

image

[Figure 1]

A list “Managers” with the following columns:

image

[Figure 2]

Fill-up the lists Managers with the following accounts (I’ve used  accounts that MS provided in their VM, you can of course use your own accounts)

image

[Figure 3]

Make sure these users are at least contributors in your web site (this is not provided in the site template).

Creating the workflow

Visual studio 2010 and Sharepoint 2010 must be running on the same computer.

Start Visual Studio 2010 and create a new Sharepoint 2010 project based on the Empty Sharepoint Project template; name the project Litware.ExpenseReport :

image

[Figure 4]

In the next window, provide your web site url, and select the option Deploy as a Farm solution : workflows generated with Visual Studio cannot run in the sandbox.

image

[Figure 5]

Add a new Sharepoint item based on the Sequential Workflow template into your project; name it ExpenseReportWorkflow :

image

[Figure 6]

Since workflows use to communicate with users by assigning tasks (more details in another post), we usually (but that is not mandatory) need a task list;the task list has been created by the site template I’ve provided;  it is good to know that if you don’t provide any task list, the Visual Studio Wizard will generate an error message:

“The Sharepoint Site at xxxxx is missing a target, task, or history list….”.

image

[Figure 7]

So don’t worry too much about this for the moment, but keep that in mind.

Let’s go to the next step.

In the next window, you will have to specify if you want your workflow to be associated with a Sharepoint list item (List workflow) or not (Site Workflow which is a new option in Sharepoint 2010); since our expense report will be stored in a Sharepoint list (Expense Reports), we will select List Workflow.

image

[Figure 8]

Click Next.

In the next windows, select the list you want your workflow to be associated with : in our case: Expense Reports.

image

[Figure 9]

[Note: the workflow History list is an hidden list that contains key information about each workflow event including date, status, participant, and description;the Visual studio 2010 wizard will create it for you.By default the workflow history is not visible 60 days after a workflow completes or is cancelled;you will find more information about the Workflow Auto Cleanup Job here]

Click Next.

This window will allow us to specify some association parameters : when we associate a workflow to a list (we can associate it manually, I will illustrate that later or via the Visual Studio 2010 wizard), we can specify if we want the workflow to start manually (the user will manually start the workflow), or if we want the workflow to start when a new list item is created, or if an existing item is changed.

(We can change that later if we want).

Let’s keep the following options:

image

[Figure 10]

Click Finish.

The workflow will now show-up in the workflow designer:

image

[Figure 11]

The first activity (step) of a (Visual Studio) Sharepoint workflow must be an OnWorkflowActivated activity.

Go to the Solution Explorer, you will see that a new Sharepoint Project Item (of type workflow) has been generated: the associated .Net code can be found in the file ExpenseReportWorkflow.cs which is also a .Net class; we usually call this class a workflow template.

image

[Figure 12]

Let’s build & deploy what we have done so far:

Go to the Build menu and select Deploy Solution.

This generated code is called a workflow template; several instances of the same workflow template can be associated to the same list or to different list in the site collection.

Go to your web site, click on the list Expense Reports, go to the list settings and on the ribbon, click on the Workflow Settings button as illustrated here:

image

[Figure 13]

The workflow settings window will show up, with a list of available workflow templates (our workflow template is available because it is encapsulated in a Sharepoint feature with a scope Site (site collection); more about this later.

image

[Figure 14]

In this window you can associate your workflow template with the current list; clearly here, we already have one association (done by Visual Studio 2010): its name is Litware.ExpenseReports-ExpenseReportWorkflow, but you can create other workflow associations by clicking on “Add a workflow” hyperlink; you can display the list of the available workflow template by clicking on the Drop Down list.

Click on the Litware.ExpenseReports-ExpenseReportWorkflow association link.

image

[Figure 15]

Click OK.

Go to your Expense Report list, add a new expense report:

image

[Figure 16]

Save it; since the option start when a new item is created as been checked (figure 15), a workflow instance will be created from our workflow template.

Indeed, if you go back to the list view, you will notice a new column with the workflow association as a name:

image

[Figure 17]

If you click on the Completed hyperlink, you will visualize the workflow status/history/tasks in the workflow information window:

image

[Figure 18]

Using workflow activities

A workflow is a set of steps; each step is an activity. Activities are .Net classes; we can reuse existing activities and we can create our own custom activities;I will illustrate how to do that later.

The workflow Foundation (WF) infrastructure is a set of assemblies that can be visualized in the References folder of our Visual studio 2010 project :

image

[Figure 19]

As part of the .Net framework 3.5, Workflow Foundation (WF) provides several  interesting activities that can be used in Sharepoint (not all WF activities can be used in Sharepoint, however); these activities are provided in the System.Workflow.Activities assembly (figure 19).

The Sharepoint team also provided more specific Sharepoint activities : you will find them in Microsoft.Sharepoint.WorkflowActions assembly; this assembly can be found in the 14\ISAPI folder and is referenced by default in our Visual Studio 2010 project (figure 19).

These activities can be dragged & drop to the Visual Studio Workflow designer surface from the toolbox (select the panel Sharepoint Workflow):

image

[Figure 20]

[ Note For Workflow Foundation aficionados: Most activities are either activities derived from HandleExternalEventActivity which is a basic Workflow Foundation activity waiting for an Event or CallExternalMethod which is an activity calling a class implementing an interface (this class is called “local service”).

The communication between the WorkflowRuntime host (which in our case is Sharepoint) and the workflow follows the usual pattern defined by the Windows Workflow Foundation team:

  • Sharepoint will communicate with the workflow by sending events
  • The workflow will invoke Sharepoint by calling a method on a dedicated interface that will redirect the call to the Sharepoint api.

The interfaces we will have to deal with are defined in the Microsoft.Sharepoint.Workflow namespace of the Microsoft.Sharepoint assembly and are decorated with ExternalDataExchangeAttribue (see the Sharepoint Foundation sdk):

  • ISharepointService
  • IListItemServices
  • IListItemByKeyService
  • ITaskServices
  • IWorkflowModificationservice
  • IWorkflowWebProvider

 

[end of Note]

Just to make sure our basic workflow can at least say “Hello World”, we will drag and drop a LogToHistoryListActivity to the workflow surface:

image

[Figure 20]

Now in the Workflow Designer, select the logToHistoryActivity, go to its property page, and in the HistoryOutcome property type Hello World.

image

[Figure 21]

Deploy the solution as we did previously (Menu Build-Deploy Solution).

Go to the Expense Reports list, select the expense report we created before, and click on the Workflows button on the ribbon.

image

[Figure 22]

The start workflow window will show up:

image

[Figure 23]

Click on the workflow button to start our workflow.

Go to the workflow status, and you should see the Hello World message as expected :

image

[Figure 24]

Workflow Feature

Workflows generated from Visual Studio are always logically packaged as Site collection features; if the feature is not activated the workflow template cannot be used !

Workflow generated by Visual Studio are always compiled as a dll that must be installed into the GAC; therefore these kinds of workflows cannot run in the Sandbox environment, even if we can create custom sandbox activities as I will illustrate in another post.

If you take a look at the elements.xml file that describe the workflow feature, you will  find the <Workflow> element which points to the workflow class Name, as well as the placeholder for the assembly name.

New in Sharepoint 2010 is the <AssociationCategories> elements that could contain List (if the workflow must be associated with a Sharepoint list), or Site if the workflow must be associated with a Sharepoint site), or Content Type (association with a content type, which is a very good practice).

image

[Figure 25]

The <StatusPageUrl> elements specifies the aspx page used to display the workflow status (see figure 24); we usually keep _layouts/WrkStat.aspx, but this can be replaced by a custom page if needed.

Wizard setting modification

If you want to modify the settings provided by the Visual Studio 2010 wizard, you can select the workflow Sharepoint project item, display its property page and change the settings like, for instance the Task List (or you can also cancel the Automatic association of the workflow with the lists –>see Property Auto Associate).

image

[Figure 26]

If you try to modify the task list for instance, a new wizard window will show-up.

image

[Figure 27]

Code

if you open the file ExpoenseReportWorkflow.cs you will see a class derived from SequentialWorkflowActivity, which is the root class for Sequential workflows; there are 2 kinds of Workflows (Sequential workflows and State Machine workflows, I will illustrate that in another post)

image

[Figure 28]

If you open the Designer code, you will notice that each activity is a member of the class:

image

[Figure 29]

In the next step we will enhance this workflow.

21 responses to “Step by Step Tutorial : Creating workflows for Sharepoint 2010: Introduction (step 1/15)

  1. Hello Luca,

    Your posts about SP WF are great and very useful. I am very pleased to see this post about WF for SP2001, but I cannot see link for step 2 on this site.

  2. hello sergeluca,
    your tutorial is very interesting and easily understanding. i’m waiting for the next step.thanks a lot.

  3. Hi it is very help full guide with micro steps, that will help to workflow developer because u have supplied all info that we don’t know well and to know that we need much experience and good practice thanks for sharing your strong knowledge.

Leave a reply to Alejandra Cancel reply