Recently I got a requirement that some of the running workflow in a list need to be closed. I received the clear instruction that, those workflow should not be canceled and status should say 'Approved'. Basically there are some old items in the SharePoint list and these items have a running workflow associated with it. Since those list items are old, there is no point to go through a approval cycle.
After doing a impact analysis, I realized that the workflow status is depending on a task. Therefore if I close the task the associated workflow will complete automatically.
I tried the following piece of code. Here I am trying to update the task associated with the workflow programmatically.
But, to my surprise this is not working. Meaning after completing the task, the status of the workflow does not changed. The OnTaskChanged() method not called within the workflow and the status still showing 'In Progress'. I have to look for an alternative approach.
Finally, I found that the AlterTask of SPWorkflow class is correct way to update a workflow task. The following piece of code solved the purpose.
8 comments:
SPWorkflowTask task = item.Tasks[0];
// alter the task
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1;
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)objWeb.Language, false),"WorkflowTaskStatusComplete", new object[0]);
// Mark the entry as approved
MessageBox.Show(SPWorkflowTask.AlterTask((task as SPListItem), ht, true).ToString());
objWeb.Update();
Add these to lines: ('FormData' is the hidden name for Outcome for a workflow task
ht["FormData"] = "Completed";
ht["TaskStatus"] = "Approved";
Hi there,
great post, thanks a lot. Unfortunally i am a complete SharePoint newbie, maybe you could give me some advice how to start implementing the code? I want to have a button in a dataview. If the user clicks the button, the workflow task should be updated to "completed".
Greetings
Christian
Hi,
This code does not work for me. item.Tasks.Count return 0 :(
here is my code:
foreach (SPListItem incidentItem in incidentList.Items)
{ incidentItem.Web.AllowUnsafeUpdates = true;
Hashtable data = new Hashtable();
data[SPBuiltInFieldId.Completed] = "TRUE";
data[SPBuiltInFieldId.PercentComplete] = 1;
data["FormData"] = "Completed";
data[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)incidentItem.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]);
SPWorkflowTask.AlterTask(incidentItem, data, false);
incidentItem.Web.AllowUnsafeUpdates = false;
}
and i got the same exception...
This task is currently locked by a running workflow and cannot be edited
Hi,
This code does not work for me. item.Tasks.Count return 0 :(
here is my code:
foreach (SPListItem incidentItem in incidentList.Items)
{ incidentItem.Web.AllowUnsafeUpdates = true;
Hashtable data = new Hashtable();
data[SPBuiltInFieldId.Completed] = "TRUE";
data[SPBuiltInFieldId.PercentComplete] = 1;
data["FormData"] = "Completed";
data[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)incidentItem.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]);
SPWorkflowTask.AlterTask(incidentItem, data, false);
incidentItem.Web.AllowUnsafeUpdates = false;
}
and i got the same exception...
This task is currently locked by a running workflow and cannot be edited
Hi,
Great post, but unfortunately I have got "This task is currently locked by a running workflow and cannot be edited" too...
I just find this: http://sams177.blogspot.cz/2010/08/programmatically-complete-workflow-in.html
I guess that it might be helpful...
Do not call Update() on listitem, you will end up locking your task with current workflow.
http://aarebrot.net/blog/2011/10/how-sloppiness-and-spworkflowtask-altertask-could-inadvertantly-lock-your-workflow-task/
Post a Comment