Workflow locks because of OnWorkflowItemChanged event handler

Version française.

SharePoint workflow functionality comes with a set of specific activities and events to leverage SharePoint specific actions.

One of them is OnWorkflowItemChanged, designed to fire when item which started the workflow was modified.

 

The Problem

However, this event causes an unexpected behavior that locks the workflow instance in the content database for a while when item is modified.

Let’s check the following simple workflow:

Basically, it creates a task, wait for the task to be modified and then simply terminates.

Notice the OnWorkflowItemChanged event handler is in a branch that is always false (condition is ‘1 == 2’), so it should never be processed.

Now imagine this scenario :

1) you create a new item so that your workflow instance starts

2) After workflow instance has started, you edit the item.

3) You edit the task

This action should wake up the workflow that would delete the task and terminate.

However, because of OnWorkflowItemChanged event, the workflow will be locked at step 2 (as soon as you edit the item). So you must wait for a while (impossible to reduce) before it can be waked up by OWSTIMER and resumes.

A side effect you may notice is this error message you will get if you try to edit the task:
“This task is currently locked by a running workflow and cannot be edited.”

 

How to fix it

1) If you wait long enough, workflow should always resume in the OWSTimer.

2) You can modify a little bit the design of the workflow to workaround this problem:

Take a look at the same workflow with a few modifications (I removed the if condition because it was only to illustrate the problem):

Workflow OK

Here are the modifications to make it working:

  1. Add a SequenceActivity and move OnWorkflowItemChanged event inside.
  2. Add an InitializeWorkflow Activity inside the SequenceActivity
  3. Create a new correlation token in the InitializeWorkflow and set the OwnerActivityName to the SequenceActivity
  4. Use this new token in the OnWorkflowItemChanged

These few modifications make your workflow running fine, and it is not locked anymore.

Yvan Duhamel, Support Escalation Engineer

Comments

  • Anonymous
    June 11, 2010
    The problem is a bit larger. Workflow item can be sometimes changed before task changing, but sometimes item will not be changed at all...In case when no item changing appears - onWorkflowItemChanged activity locks the workflow again because it waits for event which never appearing... This is very strange bug...it permanently appears on my developing server, but never appears on client's server...on client's server i easily disable onworkflowitemchanged activity and it works always, even when i change workflow item...

  • Anonymous
    July 30, 2011
    Yes this problem have a lot of time and today this not have been resolved, I think that is a desing error o fail into wwf.

  • Anonymous
    December 10, 2013
    Can't you use callExternalActivity / HandleExternalActivity in this case instead?

  • Anonymous
    December 19, 2013
    Hi I got this exception, how did you do?  CorrelationToken token = new CorrelationToken();            token.OwnerActivityName = "ReviewActivity";            token.Name = Guid.NewGuid().ToString();            createReviewTask1.CorrelationToken = token;            onTaskChanged1.CorrelationToken = token;            completeTask1.CorrelationToken = token; Unexpected System.InvalidOperationException: This operation can not be performed at runtime.     at System.Workflow.ComponentModel.DependencyObject.SetValueCommon(DependencyProperty dependencyProperty, Object value, PropertyMetadata metadata, Boolean shouldCallSetValueOverrideIfExists)     at System.Workflow.ComponentModel.DependencyObject.SetValue(DependencyProperty dependencyProperty, Object value)     at System.Workflow.Activities.CallExternalMethodActivity.set_CorrelationToken(CorrelationToken value)

  • Anonymous
    November 18, 2014
    The comment has been removed