Skip to main content

Cloud

CompleteTask Activity Throws Exception

When developing a SharePoint workflow, if you don’t set the TaskId property declaratively on the CompleteTask activity (e.g. if you’re following this recommended approach to handling task-related activities), you might find yourself with the following friendly exception:

System.InvalidOperationException: Correlation value specified does not match the already initialized correlation value on declaration [correlationToken] for activity [completeTaskActivityName].

And as the exception suggests, the correlation token is a reasonable thing to check.

In my case, however, I had a CreateTask activity and an OnTaskChanged activity — both of which worked fine — but the CompleteTask activity (using the same correlation token as the other two activities) kept throwing that exception.

This post in the Microsoft forums tipped me off.

The issue isn’t with the correlation token: it’s with the value for the TaskId property. But if you’ve created the TaskId in code and not referenced it declaratively, then how do you find the TaskId for the task you want to complete? You could always store the value in a private member, but it’s annoying to have to do that.

There’s good news: it turns out the CorrelationToken class has a set of properties hanging off of it that magically get populated with the value for the TaskId.

Here’s the code that worked for me:

private void completeTask1_MethodInvoking(object sender, EventArgs e)
{
CompleteTask task = sender as CompleteTask;
List<CorrelationProperty> tokenProperties =
(List<CorrelationProperty>)task.CorrelationToken.Properties;

// in this case, I know the property index is zero: refactor to a "find"
task.TaskId = (Guid)tokenProperties[0].Value;
}

As noted in the comment, my example here is a little dangerous. In my case, the "taskId" property was always at ordinal zero, but I haven’t done enough research to see if there are other things that end up in that list. One way to improve this example would be to implement a predicate that allows searching on the property name.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Matthew Morse

More from this Author

Follow Us