March 05, 2012

Creating SharePoint 2010 Workflows: Starting with Visual Studio 2010 Part 1

Now, in this series of articles, I will provide a step-by-step approach to building workflows in SharePoint 2010, using Microsoft Visual Studio (VS) 2010, SharePoint Designer (SPD) 2010, and Visio 2010.


In this first article, I will cover the basics: creating a VS 2010 project and SharePoint 2010 workflow, deploying the workflow, and understanding some basic concepts. The code we will create in this article will be upgraded in future articles. (To download the beginning code and site template that we'll use in this article, click the hotlink at the beginning of the article.)


You'll also need a virtual machine (VM) for this tutorial. I use the free 2010 Information Worker Demonstration and Evaluation VM, which you can download from Microsoft; if you prefer, you can get an instance of the same VM running in the cloud from www.cloudshare.com.


The Scenario: Submitting an Expense Report




The workflow that we are going to create is an application that allows users to submit expense reports. As soon as an expense report is submitted, a workflow will be activated and will
  • generate a globally unique identifier (GUID) for the expense report
  • determine whether the report must be submitted for approval (for amounts less than $1,000, the expense report is automatically approved and the process is completed with a status of "autoapproved")
  • find the user's manager
  • create tasks for the manager to approve or reject the expense report
  • update the expense report status to "approved," "rejected," or "autoapproved"
Before starting this tutorial, you need to create a site (based on the template that you can download from the hotlink at the beginning of this article) that has the following lists:
  • Expense Reports, with columns such as those in Figure 1
  • Managers, with columns such as those in Figure 2

Figure 1: Expense Reports list structure

Figure 2: Managers list structure
Fill the Managers list with the accounts that Figure 3 shows (I've used accounts that Microsoft provides in its VM, but you can use your own accounts.) Make sure that the users in this list are at least contributors in the site (a setting that is not provided in the site template).





Figure 3: List of managers and their direct reports




Creating the Workflow


Before creating the workflow, make sure that VS 2010 and SharePoint 2010 are running on the same computer. Start VS 2010 and create a new SharePoint 2010 project that is based on the Empty SharePoint Project template, as Figure 4 shows. Name the project Litware.ExpenseReport.




Figure 4: Creating a SharePoint project in VS 2010

In the SharePoint Customization Wizard, when prompted to provide your web site URL for debugging, enter the URL and select the Deploy as a farm solution option, as Figure 5 shows. Workflows that are generated with VS cannot run in the sandbox.

<><> <><> <><> <><> <><> <><>
Figure 5: Deploying the VS project

Add a new SharePoint item, based on the Sequential Workflow template, to your project, as Figure 6 shows. Name the item ExpenseReportWorkflow.






Figure 6: Adding a sequential workflow project item





Because workflows communicate with users by assigning tasks (I'll provide more details about this topic in another article), we usually need a task list (although it isn't mandatory). For this tutorial, the site template creates the task list. If you don't provide any task list, the SharePoint Customization Wizard generates an error message that begins "The SharePoint Site at xxxxx is missing a target, task, or history list..." (as Figure 7 shows). Don't worry too much about this for the moment, but keep it in mind.






Figure 7: Missing-list error message





In the next window, you need to specify whether you want your workflow to be associated with a SharePoint list item. If so, select the List Workflow option; if not, select the Site Workflow option, which is new in SharePoint 2010.
Because our expense report will be stored in the Expense Reports SharePoint list, select List Workflow, as Figure 8 shows. Click Next.
In the next window, select the list that you want your workflow to be associated with: in our case, Expense Reports, as Figure 9 shows. (Note that the Workflow History list is a hidden list that contains key information about each workflow event, including date, status, participant, and description. The wizard creates this list for you. By default, the workflow history is visible for only 60 days after a workflow completes or is cancelled. I'll provide more information about the Workflow Auto Cleanup job in a future article.

Click Next.


Figure 8: Choosing list or site workflows


Figure 9: Choosing lists for debugging


The next window allows you to specify how you want the workflow to start.
When you associate a workflow to a list (which you can do manually, as I will illustrate later, or by using the SharePoint Customization Wizard), you can specify whether you want the workflow to start manually (i.e., the user will manually start the workflow) or automatically when a new list item is created or when an existing item is changed. (You can change these parameters later if you want.)
Let's keep the options that Figure 10 shows. Click Finish.


Figure 10: Selecting workflow starting options

The workflow now shows up in the Workflow Designer, which Figure 11 shows. The first activity (or step) of a VS SharePoint workflow must be an onWorkflowActivated activity.







Figure 11: First workflow in the Workflow Designer


Open the Solution Explorer, which Figure 12 shows. You will see that a new SharePoint project item (of type Workflow) has been generated. The associated .NET code can be found in the ExpenseReportWorkflow.cs file, which is also a .NET class; we usually call this class a workflow template.





Figure 12: Solution Explorer showing workflow generated code

No comments:

Post a Comment