Sunday, 3 March 2013

Task locked by Running Workflow and Cannot be Edited

If you’re trying to update a task item that is linked to a running workflow using code similar to
public UpdateTaskItem(SPList taskList, int identifier, string newValue)
{
  taskItem = taskList.GetItemById(identifier);
  // Update task item
  taskItem["fieldname"] = newValue;
  taskItem.Update();
}
and receive an error stating This task is currently locked by a running workflow and cannot be edited, a simple fix is to update the built in field WorkflowVersion to the value 1 prior to calling the Update method.

public UpdateTaskItem(SPList taskList, int identifier, string newValue)
{
  taskItem = taskList.GetItemById(identifier);
  // Update task item
  taskItem["fieldname"] = newValue;
 
  Guid workflowInstanceId = new Guid(taskItem[Microsoft.SharePoint.SPBuiltInFieldId.WorkflowInstanceID].ToString());
  SPWorkflow workflow = taskItem.Workflows[workflowInstanceId];
  if (workflow != null && !workflow.IsLocked)
  {
    taskItem[Microsoft.SharePoint.SPBuiltInFieldId.WorkflowVersion] = 1;
  }
 
  taskItem.Update();
}
Setting this field to 1 allows the task item to be updated. There’s a check performed by SharePoint to see if this field’s value is set to 1 and when it’s not, the This task is currently locked by a running workflow and cannot be edited error is thrown.

No comments:

Post a Comment