Interacting with a workflow in SharePoint is relatively straight-forward if you understand the underlying process. I’ve done multiple projects in both SharePoint 2007 and SharePoint 2010 with some level of workflow interaction: starting a workflow, approving or rejecting a workflow task, or reassigning a workflow task.
Today however I reused that code on a SharePoint 2010 environment but the Task Approval didn’t kick in, rather the workflow seemed to ‘hang’ indefinitely.
1 bool result = false;
2 string url = "http://devsp/Shared Documents/Sample 1.docx";
3
4 using (SPSite site = new SPSite(url))
5 {
6 using (SPWeb web = site.OpenWeb())
7 {
8 SPListItem item = web.GetFile(url).Item;
9 SPWorkflowTask wfTask = item.Tasks[item.Tasks.Count -1];
10
11 Hashtable htProps = new Hashtable();
12 htProps["TaskStatus"] = "Approved";
13
14 result = SPWorkflowTask.AlterTask(wfTask, htProps, true);
15 }
16 }
17
18 MessageBox.Show("AlterTask Outcome: " + result);