This task is currently locked by a running workflow and cannot be edited

Source: Internet
Author: User

From: http://geek.hubkey.com/2007/09/locked-workflow.html

From: http://blogs.code-counsel.net/Wouter/Lists/Posts/Post.aspx? List = c04a88a9 % 2dd138% 2d4ac3% 2da2bb % 2db95c9fdd114e & id = 118

"This task is currently locked by a running workflow and cannot be edited" error occurs during spworkflow. alerttask ().

 

10/7/2009
Understanding and resolving the workflow task locking issue

[Edit; seems like VTS attributes are not picked up as I expected]

A Not so uncommon error that people encounter with SharePoint workflow is running into locked tasks. you know, the error that you get when working with workflow tasks, usually on your developer machine. you might get the error message "this task is currently locked by a running workflow and cannot be edited. ".

Why do I know it is not uncommon? Well, if you use my common-o-meter you'll see for yourself:

That is a lot of hits!

So keep people have this issue with task locking. generally it goes like this. A developer creates a workflow that issues a couple of tasks. next the developer fiddles with his code to get it right. then, returning to his test workflow, he finds that when editing a task and clicking OK, The SharePoint UI informs him of the fact that the task is locked and cannot be edited.

So, what is going on here? There are a number of items that show up searches for this error message, but never a good explanation on why you got there in the first place. More symptom management that bug fixing! Well, the first thing to note is that the error is absolutely correct. The task is locked, but why is it locked, and was it not unlocked appropriately? Despite what happens people think this has nothing to do with your DLL versions, at least not as directly as you might find written and guessed at. Here's the story.

How workflow tasks are locked

The first thing to realize is that when SharePoint workflows alter tasks there needs to be some sort of locking behavior on tasks so that you will not accidentally create race conditions and update a task simultaneously, the one update overwriting the other. typically database level locks are used but for Sharepoint workflow tasks however a more simple, business-layer type lock suffices. since SharePoint workflow is about humans and not about maximum near real-time performance the chance of collisions is low enough not to be worried about this. the workflow runtime in SharePoint locks tasks by setting a field and persisting that to the database. it then checks on the field value to determine whether it is locked. you can actually see the code that does this. the spwinoeitemeventreceiver implements the itemupdating and itemupdated events. in the itemupdating you can find code similar to the following pseudo code

If workflowversion for item not equal to 1
Throw locked Error
Else
Place lock (set workflowversion on item to value not equal to 1)

How the lock placement is actually implemented is that the workflowversion is set to the value in _ uiversion, which contains a value indicating the major/minor version of the task. Why _ uiversion? It ties the lock to a specific version of the list item, and Versioning is enabled on Workflow task lists. this probably allows the locks to be bypassed by other code inside SharePoint depending on the version. (By the way: Do * not * use this knowledge of Internals in production code)

The next interesting question is where the lock is released. the itemupdated event facilitates this. when the task lock is detected in the itemupdated event it is routed to the spworkflowmanager. this manager runs code to dehydrate and startup the workflow (which was persisted to the database while waiting for the task change to occur ). the spworkflowmanager uses the spwinoehostservices workflow service to unlock the task in the postworkitemdequeue method and runs the workflow.

Running into the locking issue

So, the issue is that the lock is still there even though it shoshould have been released in the itemupdated event. clearly, the itemupdated event is where the issue lies, and like all bugs in life, you did it, and not the framework! (Hope that does not come as a shocker to you) There is only one aspect of the locking that you can control, and that is the persistence and hydration of your workflow to and from the database. this is exactly what is causing the bugs. when the itemupdated event fires and tries to de-serialize your workflow there might be an exception during the hydration of your workflow object. this error is diff Icult to see since it is happening in non-user code based on an asynchronous event. When that error occurs, the task unlocking Code does not run!

The general flow of events to create this issue goes something like this.

  • Developer designs a workflow which creates a task.
  • Developer tests the workflow, and runs it up to the task change activity, meaning that the workflow is now serialized in the database waiting for a task change to occur.
  • Developer spots a bug, and updates the workflow in such a way that de-serialization breaks.
  • Developer updates the task through the browser to continue the workflow.
  • Runtime bumps into the de-serialization error, and cannot continue, hence the task unlocking Code does not run, and the task is locked for all eternity.

A common de-serialization issue that you might create is a change in the activity structure, or the addition of control fields in your main workflow class.

Preventing the locking issue

Now that we have a clear understanding of the issue, there are too things you can about it. on Development I 'd go for re-running the entire workflow (at least when it is not too big ).

On production, it is even easier:

Do not upgrade until all running workflows are complete

You shold quiesce a workflow and when all running workflows have completed, update. Or, when you need to have the business logic available during the quiescing, you can only create a new workflow.

Hope it helps!

 

 

Posted at 11: 50 am by Wouter van Vugt | permalink | email this post | comments (0)
Locked Workflow

I 've periodically come into SS 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...

Code

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

 

Anonymous said...

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.