Sunday, September 11, 2011

Create a sequential workflow using Visual studio 2010 in SharePoint 2010

Here we will discuss how to develop a sequential workflow using Visual studio 2010 in SharePoint 2010. Microsoft provides different project templates to work for SharePoint.
For this first open Visual studio 2010 and then File -> New Project.Then from the new project dialog box choose Visual C# and then Select SharePoint and from there select 2010 and Choose Sequential Workflow template and give a name in the Name box as shown in the figure below(SequentialWorkflow.png) and click on OK.
In the next step provide the local debugging URL and you will able to only deploy as farm solution is selected
because you can not deploy a workflow as sandboxed solution. See the figure below (SequentialWorkflow_01.png).
And click on next. In the next step give the name of workflow and select the type of workflow as shown in the figure below(SequentialWorkflow_02.png).
In the next step select the list or library you want to attach the workflow and select where you want to show the history list you want to store as shown in the figure below (SequentialWorkflow_03.png).
The Workflow history list is used to store all events that occur while workflow is running.Here also you can check the check box if you want to automatic associate the workflow to the selected list or library. In the next step you can choose when you want to start the workflow as shown in the figure below(SequentialWorkflow_04.png).
Now your visual studio should look like the figure shown below(SequentialWorkflow_05.png).
In the solution explorer there is one folder for Feature and one folder for Package. The package folder will hold the .wsp file when you package or deploy the project. Elements.xml: This file describes the workflow to SharePoint and it stores the name and description of the workflow, any forms that it uses, and other information.Workflow1.cs:These are the files that make up the activities and code for the workflow.

Now Drag and drop a CreateTask activity from the Toolbox to the Workflow designer, right below the onWorkflowActivated1 activity. There is a red mark in the activity which shows that we need to set a mandatory field in the workflow as shown in the figure below(SequentialWorkflow_06.png).
Then go to the activity properties and give the corelationtoken.You have to use a unique correlation token for each task you are referencing in a workflow. And then expand the correlationtoken property and set the OwnerActivityName to Workflow1 as shown in the figure below(SequentialWorkflow_07.png).

Now also the red mark removes. Then find the TaskID and click on ..., it will open the Bind dialog. The Bind dialog is used to link a workflow property to a field or property in the code-behind file. This can be either an existing member or you can choose to create a new one. Here Click Bind to a new member tab. Type “taskId” in the New member name textbox. Select Create Property radio button. Then click on OK as shown in the figure below (SequentialWorkflow_08.png).
Next we need to do all these steps for TaskProperties property. In the next step double click on the CreateTask1 Activity ant will generate createTask1_MethodInvoking method. In this method, initialize taskId and taskProperties.
Here we will assign the task as shown below:
private void createTask1_MethodInvoking(object sender, EventArgs e)
            taskId = Guid.NewGuid();
            taskProperties = new SPWorkflowTaskProperties();
            taskProperties.AssignedTo = "domainname\\username";
            taskProperties.Title = "Task assigned from the sequential workflow";
After that F5 to deploy the web part, once the deployment finish go to the browser and click on the particular list. Click on the add new item and when you submit the form the breakpoint hits and when you will refresh a new item will be added to the list with name Task assigned from the sequential workflow.