Search This Blog

Monday, February 13, 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 



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

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

14 comments:

  1. oy images are not looking clear unable to get it

    ReplyDelete
  2. Hello there I am so delighted I found your blog, I really found you by accident, while I was researching on Bing for something
    else, Regardless I am here now and would just like
    to say kudos for a remarkable post and a all round exciting blog (I
    also love the theme/design), I don’t have
    time to read through it all at the moment but I have book-marked it and
    also added in your RSS feeds, so when I have time I will be back to
    read more, Please do keep up the excellent jo.


    Here is my web site; affiliate Guide

    ReplyDelete
  3. Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn't show up. Grrrr... well I'm not
    writing all that over again. Anyways, just wanted to say excellent
    blog!

    Feel free to surf to my homepage - internet marketing and seo company

    ReplyDelete
  4. Nice location. I like it allot… but why is it so brief?


    my blog; online business opportunities

    ReplyDelete
  5. Do you think that quite some there. I did a search on
    topics and there, most agree around the
    blog.

    my web site work from home business

    ReplyDelete
  6. You produced some prestigious tips there. I did a search on the matter and found most people
    will agree with your weblog.

    My web page :: günstig urlaub

    ReplyDelete
  7. Super-Duper web site! I am loving it!! Will come back once again - acquiring you feeds also, Thanks.


    Take a look at my web site ... Scotty Billiot - User - the Data Hub

    ReplyDelete
  8. As a Newbie, I am often exploring on-line for articles which will
    help me. Give thanks you

    Here is my blog post :: 0 comments

    ReplyDelete
  9. I commonly usually do not respond to messages, but on this case.
    WoW! '

    Here is my page günstig reisen mit kindern

    ReplyDelete
  10. I generally don't reply to messages but this was the reason. WoW:)

    My web page - mallorca reisen

    ReplyDelete
  11. selective occupation information. I want to deal with it much more?


    Also visit my web page :: nach türkei reisen

    ReplyDelete
  12. ohh ... Final scene, but seriously? /? : P

    Visit my site http://www.krankenversicherungvergleich24.com/

    ReplyDelete
  13. : O So a great deal information: O ... This really is 1 of essentially the most wonderful site .
    .. Dude: D

    Here is my blog: website hosting uk

    ReplyDelete
  14. Hi, I am not able to get past the "the sharepoint site is missing a target" error. I get it after the type of reusable workflow template dialog box. Please help.
    Thank you,
    Bindu

    ReplyDelete