Thursday, October 13, 2011

Get TaskID in SharePoint custom Visual Studio workflows

When you are working with custom workflows implementing in Visual studio, you might be sending the emails when a task is created. By default SharePoint sends an email when a task is assigned to a user. But, if you want to customize the email body and subject, then you will use send mail activity and will give link to the user. Here the problem starts.
In CreateTask activity in Visual Studio you cannot get the taskID as the task is not yet created. What to do??? So, to get the task identifier we have to implement the below approach.
  1. CreateTask Activity
  2. TaskCreated Activity 
  3. SendMail Activity
  4. OnTaskChanged Activity
  5. TaskComplete Activity
Remember, until unless the Activity TaskCreated is executed the TaskID will not be available in workflow context. The main reason behind is, the task created information is not caught by SharePoint workflow anywhere until we run this activity. So, in the TaskCreated activity you can get the AfterProperties of the task and in those properties you will get the recent created task identifier which is integer.

So, note that in custom workflows which are implementing through Visual Studio the only way you can get the TaskID is AfterProperties of the TaskCreated activity. I have wasted hours when I was learning these. Hope you liked it.

6 comments:

Anonymous said...

Using this approach means you can only send an email notification for a task when a task is completed. Thats pretty useless if you want to notify users of new tasks.

Praveen said...

Hi there,
I am sorry for the mistake. I was typing wrong TaskCreated and TaskCompleted in my post. Now, corrected. Thanks for pointing it out.

MLM Software India said...

The blog was absolutely fantastic! Lots of great information and
inspiration, both of which we all need!b Keep 'em coming... you all do
such a great job at such Concepts... can't tell you how much I, for
one appreciate all you do!

amrit said...

Vinyl Banners

I like this blog very much.Thanks for sharing such a valuable information.

Kshitij said...

Thanks Pravin for sharing information. I have used CreateTask activity but somehow its not sending email. I have created workflow from SPD and used Send Email action and that worked. What could be wrong ?

Praveen said...

First thing as always, check your SMTP [This don't make any sense here as you are saying from SPD it is working.
Second, try to set the task activity send mail option to true.
Third, explicitly use SendMail activity and try to send email.

thanks
-PRaveen.