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