I've periodically come across this SPException: "This task is currently locked by a running workflow and cannot be edited" when using the SPWorkflowTask.AlterTask method, even when it seems that the workflow is not in fact locked, and is instead patiently listening for an OnTaskChangedEvent. It turns out that this exception is thrown when the WorkflowVersion of the task list item is not equal to 1, which, if you believe the error message is the same thing as checking to see if the workflow is locked. Only it isn't - apparently sometimes at least, the Workflow version is non zero and the workflow is not locked (the InternalState flag of the workflow does not include the Locked flag bits). I'm not sure why this is occurring - maybe the error message is misleading - but the following code demonstrates a dodgy sort of a workaround that I've found useful. I've no idea if this is a good idea or not, so please treat with skepticism...
using System;using Microsoft.SharePoint;using Microsoft.SharePoint.Workflow;using System.Collections;using System.Threading;namespace DevHoleDemo{public class WorkflowTask
{public static bool AlterTask(SPListItem task, Hashtable htData, bool fSynchronous, int attempts, int millisecondsTimeout)
{if ((int)task[SPBuiltInFieldId.WorkflowVersion] != 1)
{SPList parentList = task.ParentList.ParentWeb.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
SPListItem parentItem = parentList.Items.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
for (int i = 0; i < attempts; i++)
{SPWorkflow workflow = parentItem.Workflows[new Guid(task[SPBuiltInFieldId.WorkflowInstanceID].ToString())];
if (!workflow.IsLocked) { task[SPBuiltInFieldId.WorkflowVersion] = 1;task.SystemUpdate();
break;}
if (i != attempts - 1) Thread.Sleep(millisecondsTimeout);}
}
return SPWorkflowTask.AlterTask(task, htData, fSynchronous);
}
}
}


7 comments:
Thank you so much for the tip, your code did the trick. I think the reason WorkflowVersion changes is because there's a different version of the workflow dll. So when you recompile your workflow and DLL is changed in the GAC while there are still running workflows, trying to finish any of those workflows might give you "This task is currently locked.." error.
Elena.
Thank you very much. I had the same problem, your solution works fine.
How would I use this code snippet? Do you have an example of that?
You could use it as you would the SPWorkflowTask.AlterTask method, adding the attempts and timeout parameters. Here's someone who was having issues and posted some code:
http://www.serious-sam.net/serendipity/index.php?/archives/18-More-SharePoint.html
Thank u very very much! With this I was able to resurrect some workflows having randmonly this issue. What I would like is to find out the reason. Why this attribute is left with values like 1024, 8096? It's cleary a flag. I think I will take a look in the WSS 3.0 assemblies to find out when is set.
Cheers,
Tibi
The solution is really great but in our scenario the DLL was same, we never changed it but were facing the issue. Thanks anyway!!!
I'm having this problem with my workflow! Can you please give me some directions how to use the code snippet above??
Post a Comment