SharePoint 2010: Programmatically Approving/Rejecting Workflow

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.

SharePoint Designer “Start Approval Process” Workflow

SharePoint Designer “Start Approval Process” Workflow

Start the Approval process

Workflow Status Page

(don’t mind the errors, they’re about a missing outgoing mail server)

Programmatically approve the task

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


Workflow Completed (Approved)

Some things to note

  • If you have a localized site you need the translated equivalent of “Approved” and “Rejected”. For Dutch that would be “Goedgekeurd”
  • Reassigning a task can be done by specifying a new SPUser value for the “AssignedTo” value similarly to the above Approve action
  • Next post will be about some weird behaviour resulting in “This task is currently locked by a running workflow and cannot be edited” or “Note: Due to heavy load, the latest workflow operation has been queued. It will attempt to resume at a later time” messages

Follow up


Links to this post


CAPTCHA Image Validation