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 (5)
  1. Ivan Gorbadei says:

    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…

  2. ernest says:

    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.

  3. Mike says:

    Can't you use callExternalActivity / HandleExternalActivity in this case instead?

  4. Wint says:

    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)

  5. AdamantineWolverine says:

    I've noticed that its hard to find many articles on Workflows.  Even Microsoft's documentation on workflows seems pretty sparse.  

    I'm having my share of issues with the OnWorkflowItemChanged activity, myself.  

    1) OnWorkflowItemChanged requires that you use the same correlation token as the one used in OnWorkflowActivated.

    2) It seems that you can only create new tokens early in the life cycle of the workflow initialization phase.  

    Otherwise, you'll get that nasty error:

    Unexpected System.InvalidOperationException: This operation can not be performed at runtime

    Correlation tokens are created in the InitializeComponent() function of your workflow.  The code for generating those tokens is handled by VisualStudio and placed in your workflow's .designer.cs file.  

    3) I wrote a blog article about how to pass the OnWorkflowActivated token to a custom activity.…/sharepoint-windows-workflow-foundation.html

    4) At this point, though, I'm so frustrated with how OnWorkflowItemChanged behaves that I am considering just writing a SharePoint EventReceiver that updates workflow tasks related to the list item.

    I was trying to figure out why the workflow keeps locking up when the item changes, and I have found, yet, another article that has pointed the finger at OnWorkflowItemChanged.

Comments are closed.

Skip to main content