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.”
Scenario:
We need to send an email to the assigned person/group with the custom mail body with the task link.
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 cannot 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. 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.
Many solutions proposed on the net/MSDN that we cannot 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. 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:
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.


