Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#

Create a Workflow using Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.71/5 (13 votes)
5 Jul 2012CPOL3 min read 106.7K   1.5K   15   7
Creating a Workflow using Microsoft Visual Studio 2010.

Introduction

In this article we can experiment with creating a Workflow using Microsoft Visual Studio 2010. Visual Studio along with SharePoint 2010 Extensions provides sophisticated development tools to enable Workflow development.

Types of Workflows

There are basically two types of workflows inside Visual Studio:

  • Sequential Workflow
  • State Machine Workflow

Sequential Workflow once invoked continues execution until it is completed.

State Machine Workflow will have states persisted in between. The state could be continued execution between machine restarts.

In this example we are trying to create a Workflow which on activation updates the null Address column of the Manager list. (You need to have a Contact template named Manager.)

Step 1: Create Sequential Workflow Project

For the time being, we can start with a Sequential Workflow. Start Visual Studio and create a new project from the template SharePoint > Sequential Workflow.

Image 1

In the next screen select the option Site Workflow as shown below:

Image 2

In the next screen, leave the default option saying the user manually starts the Workflow. Click the Finish button to create the project.

Image 3

You will get the following screen once the project is created.

Image 4

Step 2: Create Activity

We need to create an Activity to perform our job.

What is an Activity?

A Workflow consists of a series of Activities. We can add Activities using the Toolbox. There are different types of Activities like Code Activity, SendEmail, etc. For our example we are using the more functional Code Activity.

Image 5

Drag and drop a Code Activity from the toolbox. You can locate this from the v3.0 group inside Toolbox.

Image 6

Step 3: Add code for the Activity

Now we need to add code for this Activity. Double click on the codeActivity1 item shown above. Place the following code in the appearing code view.

C#
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
    using (SPWeb web = SPContext.Current.Web)
    {
        SPList list = web.Lists["Manager"];
        foreach (SPListItem item in list.Items)
        {
            if (item["Address"] == null)
            {
                item["Address"] = "PLEASE SET THE ADDRESS!";

                item.Update();
            }
        }
    }
}

Step 4: Build and Deploy the Solution

Now we are ready to build and deploy the solution. Right click on the solution and use the Build and Deploy  command.  

Image 7

Step 5: Execute the Workflow inside SharePoint

Now we are ready to test the Workflow inside SharePoint. As the Workflow was created as a Site Workflow it will be accessible for all the Lists and Libraries. You can click the Lists link inside the site.

Image 8

Now click on the Site Workflows link. You will get the following screen.

Image 9

Before executing the Workflow, you need to create a Manager item with Address not assigned.

Click on the highlighted button and your workflow gets executed. Wait for a while and you can see the invalid manager record is updated with the message.

Image 10

This concludes our article on Workflow using Visual Studio.

Note

For debugging the Workflow, you can set a breakpoint and use the Debug command of Visual Studio. When the Workflow is executed, the breakpoint will get hit.

References

Summary

In this article we have experimented with creating a Workflow using Microsoft Visual Studio 2010. In the real world scenario Workflow Programming helps us in doing complex tasks to include in SharePoint.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sandip.Nascar31-Oct-12 6:48
Sandip.Nascar31-Oct-12 6:48 
GeneralRe: My vote of 5 Pin
Jean Paul V.A31-Oct-12 7:18
Jean Paul V.A31-Oct-12 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.