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:

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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!

    ReplyDelete
  4. 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 ?

    ReplyDelete
  5. 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.

    ReplyDelete
  6. Hi Praveen Iam getting an error when i use Infopath form as my custom Task Edit Form i was gettting an error when i try to edit the task form saying "The form you are attempting to open may be located on a different server. This form can only be opened using Microsoft InfoPath". Can you please help me to solve this issue.

    i have given the infopath form urn in elements.xml and changed the xsn deployment type to Elementsfile as in http://mstechsharing.blogspot.in/2012/03/sharepoint-2010-state-machine-workflows_2141.html

    ReplyDelete