Skip to main content
 
Go Search
Home
Categories
Bloggers
CompleteTask Activity Throws Exception
By: Matt Morse | Posted: December 19, 2007 at 7:27 AM

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.


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
Update Task inside replicator activity
By: Remya | Posted: January 29, 2009 at 9:52 PM
I am using Updatetask inside a replicator for task escalation. I tried out all the combinations but always end up with the error : "Correlation value specified does not match the already initialized ...". Any suggestions?
Update Task inside replicator activity
By: Remya | Posted: January 29, 2009 at 5:36 AM
I am using Updatetask inside a replicator for task escalation. I tried out all the combinations but always end up with the error : "Correlation value specified does not match the already initialized ...". Any suggestions?
Thanks!
By: Andrew Burns | Posted: September 25, 2008 at 8:50 AM
Yup, that worked for me too, and I couldn't figure out the problem from the error message. But I am storing the taskId in a member. Of course, it raises the question - why do I have to? Isn't this what the correlation token is for?
The hint I needed!
By: Roland D. | Posted: January 24, 2008 at 11:33 AM
Hello Matt, your post makes it possible to get a workaround! In my case I work with State machine workflows. You know, NORMALLY it should be enough to bind the TaskID to the taskID property you created for CreateTask (it is '00000000-0000-0000-0000-000000000000' by default, so you choose the one created for CreateTask) BUT there are a lot of bugs in WWF ! There is a often discussed bug (i also get with SP1) that the OnTaskCreated event does not work. Exactly spoken, the event does not get fired every time, I believe sometime the event gets fired too fast, and it get lost!) What happens then? Funnily (or not) the TaskID value in this binded value changes! And to a GUID for that I cant find a task in the sharepoint content database. So it puts a random GUID into that. Your hint showed me, the right GUID stays in the task token value. So now I have this ugly code: [CODE] private void WFTaskUpdateAfterStorno_invoked(object sender, EventArgs e) { UpdateTask oUpdTask = sender as UpdateTask; // Get the taskid (guid) of the sender String sTaskGUID_1 = oUpdTask.TaskId.ToString(); List<CorrelationProperty> oCurTokProps = null; if (oUpdTask.CorrelationToken.Properties != null) { oCurTokProps = (List<CorrelationProperty>)oUpdTask.CorrelationToken.Properties; // get the taskid, which is in the tasktoken String sTaskGUID_2 = oCurTokProps[0].Value.ToString(); if (sTaskGUID_1 != sTaskGUID_2) { oUpdTask.TaskId = (Guid)oCurTokProps[0].Value; } } else //yes, the tasktoken can uninitialized, if the OnTaskChanged event got not fired { String sCurStateNr = CurrentStateName.Substring(5, 2); // ok, now I look for the tasktoken i used in the CreateTask activity // 1 task in every state, their names are state01, state02 etc. switch (sCurStateNr) { case "01": oCurTokProps = (List<CorrelationProperty>)creTask01.CorrelationToken.Properties; break; case "02": oCurTokProps = (List<CorrelationProperty>)creTask02.CorrelationToken.Properties; break; case "03": oCurTokProps = (List<CorrelationProperty>)creTask03.CorrelationToken.Properties; break; case "04": oCurTokProps = (List<CorrelationProperty>)creTask04.CorrelationToken.Properties; break; case "05": oCurTokProps = (List<CorrelationProperty>)creTask05.CorrelationToken.Properties; break; case "06": oCurTokProps = (List<CorrelationProperty>)creTask06.CorrelationToken.Properties; break; case "98": oCurTokProps = (List<CorrelationProperty>)creTask98.CorrelationToken.Properties; break; } String sTaskGUID_3 = oCurTokProps[0].Value.ToString(); if (sTaskGUID_1 != sTaskGUID_3) { oUpdTask.TaskId = (Guid)oCurTokProps[0].Value; } else { // dont know, go home? } } // here now i can update my task like i want .. SPWorkflowTaskProperties oActiveTaskProps = oUpdTask.TaskProperties; oActiveTaskProps.ExtendedProperties[this.oWFProps.TaskList.Fields["Status"].Id] = "8 - storniert"; oActiveTaskProps.Title = "Storno from Initiator: " + oActiveTaskProps.Title; } [/CODE]
This is good information
Posted: December 20, 2007 at 3:26 AM
Both this and the last post are extremely useful, thanks Matt. And keep it up :-)
 

 About Matt Morse

Practice Manager - SharePointMatt Morse is a practice manager for PointBridge. He has over 10 years of experience in application architecture and development, working in a variety of industries, including banking, manufacturing, ... [more]

 Tag Cloud

 My Articles

 ‭(Hidden)‬ Admin Links