Friday, September 5, 2008

SharePoint Workflow using Visual Studio 2005 with Extensions

Scenario:
You need to write a basic workflow in sharepoint. Once you associate the workflow to a document library , if you upload a document into the library and start the workflow, it will make an entry into TASKS list and another entry into WorkflowTASKS list.

Assumption:
You have Visual Studio 2005 and Extensions for SharePoint installed.

Solution:
01. Open Visual Studio 2005
02. Create new C# Project of type 'Sequential Workflow Library' from SharePoint node
03. Name it 'TestWorkflow'
04. Visual Studio Extension will create some files for you required for deployment.
05. Open 'Feature.xml' and modify it as given in sample below.
06. Open 'Workflow.xml' and modify it as given in sample below.
07. Now open the 'Workflow1.cs'

08. Add a 'CreateTask' activity below 'OnWorkflowActivated'
09. Add a 'LogToHistoryTaskActivity' activity below 'CreateTask'
10. Open 'CreateTask' properties window
11. Click on browse button for taskID > Bind to a new Member > Create Field > Name it 'taskId' > OK
12. Click on browse button for taskProperties > Bind to a new Member > Create Field > Name it 'taskProperties' > OK
13. Double Click on 'CreateTask' and following lines of code

taskId = GUID.NewGUID();
taskProperties.Title = "Task created by workflow" ;
14. Click on 'LogToHistoryTaskActivity' activity and set the
History Description to 'Task Created'
15. Add 'OnTaskChanged' activity
16. Set correlationToken to 'taskToken' and TaskID to 'taskId' , TaskProperties to 'taskProperties'
16. Add a 'CodeActivity'
17. Double click and enter the following code
SPList item = workflowProperties.Item;
SPList task = workflowProperties.TaskList.GetItemById(taskProperties.TaskId);
item.Title ="Task updated by workflow";
item.SystemUpdate(false);

18. Add a 'DeleteTask' activity and set its correlationToken to 'taskToken' and TaskID to 'taskId'

19. Strong Name the project
20. Build the project
21. Run the 'Install.bat'
22. Now this workflow will be available on your local machine for use.

Feature.xml:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="{NewGUIDWithBraces}"
Title="Test Workflow"
Version="1.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="workflow.xml"/>
</ElementManifests>
<Properties>
<Property Key="GloballyAvailable" Value="true" />
</Properties>
</Feature>
Workflow.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
<Workflow Name="My workflow"
Id="NewGUIDWithOUTBraces"
CodeBesideClass="TestWorkflow.Workflow1"
CodeBesideAssembly="TestWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ReplaceITWithKey"
>
</Workflow>
</Elements>
Article:
http://msdn.microsoft.com/en-us/library/ms580283.aspx
http://blogs.msdn.com/nikhil/archive/2008/01/19/sharepoint-workflow-feature-in-vs-2008-part-ii.aspx#8584351

0 comments: