Friday, May 8, 2009

Modify tasks blocked by a workflow

Sometimes using tasks on workflows we wander to modify their properties "manually".
But when we update the task item, we get an Exception reporting that the task is blocked by a workflow.

Here is the way to sort around this problem:
First of all we have to get the ItemID of the task inside the TaskList.
To know how maybe you would like to take a look here.

Then only we have to implement a little function that allows us to modify the task.
On this example, the task is modified to set as Completed.


private void CompleteTask(string taskID)
{
workflowProperties.Web.AllowUnsafeUpdates = true;

foreach (SPWorkflowTask task in workflowProperties.Workflow.Tasks)
{
if (task.RecurrenceID == taskID)
{
task["Status"] = "Completed";
task["PercentComplete"] = 1.0f;
task[SPBuiltInFieldId.WorkflowVersion] = 1;
task.SystemUpdate();
}
}

workflowProperties.Web.AllowUnsafeUpdates = false;
}

No comments:

Post a Comment