In The Mix

As a SharePoint architect I have the business behind me and the Developers and IT Pro on my shoulders.

SharePoint Task Invalid BeforeProperties Fix June 26, 2009

Filed under: SharePoint — fmuntean @ 4:18 pm

When working with tasks in a SharePoint workflow project you can get an event when a task changed but is hard to actually detect what changed as the event does not return enough information.

 

The following picture shows the regular flow to work with a task in workflow framework: image

So the minimum activities required are:

  • CreateTask to create the task
  • While  to wait until the task is considered completed
  • OnTaskChanged to receive any changes to the task
  • CompleteTask to complete the task and continue.

All the task related activities needs to be bind to the same correlation token. If your workflow will have multiple workflow make sure that you are creating multiple correlation tokens for each task.

  Now there are few other properties that are used during the lifetime of the task. I will only mention here the properties that are specific to the problem at hand

CreateTask activity needs to have the TaskProperties bind so it can use it to create the task item.

OnTaskChanged activity needs to have both AfterProperties and BeforeProperties bind so it can fill with values when your code is called.

 

Now the problem is that when the OnTaskChanged invoked event gets called the BeforeProperties seems to not contain any data where the AfterProperties seems to contain the current information.

Jus taking the example of checking to see if the task was reassigned by using the AssignedTo property for before and after properties as miss-directed by the MSDN and SDK does not work because the BeforeProperties.AssignedTo is null most if not all the times.

So what can we do now?

Let’s think how else we can get the before properties:

When we create the task we set the TaskProperties.AssignedTo to a person or group that we want to execute on the task. First time when the task is changed in any way we get the event with AfterProperties. Now we know if the AssignedTo was changed by checking if it is different than the original one that we specified when we created the task. If after that we copy the AssignedTo from AfterProperties into the TaskProperties when the task gets changed we can detect if the AssignedTo changed without using the BeforeProperties at all.

This is my solution:

  1. Forgot about BeforeProperties.
  2. Each time when a task gets changed detect the changes by checking AfterProperties against TaskProperties.
  3. After you execute the necessary code into the change event copy all necessary properties from AfterProperties back to the TaskProperties (this is your before properties now that you cache inside your workflow)
  4. Do not just  blindly assigned the AfterProperties over the TaskProperties as it will not contain all the properties all the time. AfterProperties contains only the task item properties that have changed so you will need to manually parse it back into the TaskProperties.

If you are still with me and your head did not explode over this explanation drop me a line.

Advertisements
 

6 Responses to “SharePoint Task Invalid BeforeProperties Fix”

  1. Frank Says:

    Can you be more clearer like my mind has just gone BOOM!
    with this, a sample code will do Thanks.

  2. Colin Devon Says:

    I know this isn’t a tutorial but I’m trying to use ExtendedProperties to update the workflow item but the ExtendedPropertied[“CustomField”] won’t work as it is passed back from custom aspx file as a hash key.

  3. fmuntean Says:

    Hi Colin,

    Please use “Contact Me” link from my blog and give me some more detailed info and maybe some code to be able to help you further.

    Florin

  4. Mo Says:

    Hi,

    Very appreciate if you could help me on this.
    I’ve 8 custom workflows developed using VS2008. All of a sudden, there are delays to process the tasks. A task should go to a second level approval once the first approver approves it. Now the second task is created after around five minutes. How can I debug the issue.
    I did a lot of search but it did not help. I’ve MOSS 2007 with SP2 installed.

    Thanks very much.
    Mo

  5. shiva Says:

    Hi,
    I guess this particular solution might be the soln to my problem. Can you plz be a little more precise abt the soln…? a sample code would be really helpful.

    Regards,

    Shiva

  6. fmuntean Says:

    Hi Mo,
    The workflows are used for long running process where actually 5 minutes is not a problem.

    Your problem is a load issue. Let me explain.
    Each SharePoint server can process only 15 workflow actions at a time. The more active WF you will have the longer will take for the WF engine to pickup and execute a task. The easy way out is to add more servers in the farm that will be able to process more activities in the same time.

    You can use the “contact me” to give me more details and I see what I can do to help you with your situation.

    Florin


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s