Friday, September 5, 2008

SharePoint Workflow using Visual Studio 2005 with Extensions

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.

You have Visual Studio 2005 and Extensions for SharePoint installed.

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";

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.

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="{NewGUIDWithBraces}"
Title="Test Workflow"
xmlns="" >
<ElementManifest Location="workflow.xml"/>
<Property Key="GloballyAvailable" Value="true" />
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="" >
<Workflow Name="My workflow"
CodeBesideAssembly="TestWorkflow, Version=, Culture=neutral, PublicKeyToken=ReplaceITWithKey"