Solved: Get TaskId in Visual Studio Workflow without using On Task Created activity

Common Blog

Most of SharePoint IT devs who are working on implementing the Visual Studio Workflows are looking for the solution of the problem “How to get the current created task id in the workflow to send email once task assigned/created to a user/group.” There are no simple workarounds till yesterday for this problem but now we have a solution available and that too without using the OnTaskCreated activity in the workflow.

We need to send an email to the assigned person/group with the custom mail body with the task link.

Solutions Tried:
Many solutions proposed on the net/MSDN that we can not get the TaskId of the item without using the OnTaskCreated activity as this is the activity will trigger once the task is created in the database. The sequence of execution we should follow is mentioned here. So, we have added the OnTaskCreated activity in the workflow. But, it never hit when we debug the workflow. This is because OnTaskCreated is handled by the timer job and there is a delay in loading the TaskId into the workflow. Most of the cases we observed its end up of delaying the workflow to 15-20 minutes. (This is also not sure, sometimes it may return TaskId or sometimes may not.) So, this is not the ideal solution we should use in the workflow.

Next, for immediate resolution we have tried of using the Item Created event receiver on the Task list. So, whenever a task is created the event triggers and we are sending email. But, this needs additional coding/package/maintenance.

Later, we have got a solution recommended from Microsoft employee and it solved the problem without writing extra line of code. This is the best and recommended solution.

Final Solution:
In Visual Studio Workflow, we have used the activities in the sequence:

  • First, disable or delete the OnTaskCreated activity from the workflow.
  • Right click on the CreateTask activity -> Properties window. You will see a property field “ListItemId”. Click on the field to open properties window.
  • From the properties window, click on the second tab “Bind to a new member”.
  • Enter the name of the member/field and then select the option “Create a Field” and save your changes.
  • Drag and Drop a Code Activity next to the CreateTask activity. And in the activity access the created member from above step which has the newly created taskid. And you can access the TaskID in your logic.

Finally, the complete solution including the steps shown below:

Note: From above diagram, the step “ReviewerTaskCreated (OnTaskCreated)” activity is disabled in the workflow.

This is a great and very simple problem to the complex problem. Now, a simple workflow is doing the job, no event receivers or no delays in the workflow. Thanks much to Teja for posting this wonderful solution.


Related posts

Challenges bring the best out of us. What about you?

We love what we do so much and we're always looking for the next big challenge, the next problem to be solved, the next idea that simply needs the breath of life to become a reality. What's your challenge?