I came across a very interesting requirement with respect to “Tasks” in SharePoint but before we get into the requirement lets get into few details first.

As most us are aware of the fact that when a task item is created, either directly in the task list or through a Workflow an alert email would be sent saying a task has been assigned to you (provided alerts are enabled). When we look at that email in Outlook we see an additional control in the ribbon “Open this Task” under the group “Open” as shown in the figure

Now let me explain the requirement: We have a custom workflow built using sequential workflow template in Visual studio 2010. The customer is not convinced with the details in the email body generated by SharePoint.

we have to customize the body and subject as per their request

Retain that Ribbon control “Open this Task” when the email is sent

If possible, have the “From Address” in the email to be the person who initiated this workflow

That is all what was requested, looks very reasonable, simple and straight forward. When I started implementing it my first impression was it is not as simple as it looks. Later on when I went through different approaches I felt I wasn’t very wrong about this requirement and eventually it turned out be a very simple solution. As they often say “All things are difficult before they are easy”. So I thought I would post my approaches and the final solution.

I have used “SPWorkflowTaskProperties” class in the Task Created Event and sent it two properties “SPWorkflowTaskProperties.HasCustomEmailBody = true” and “SPWorkflowTaskProperties.EmailBody= <My Custom HTML Email Body> ” With this I can satisfy requirement 1 & 2 but not 3 (listed above). Also if I were to create a task and if I want to send an email using Event Handler on item created I may not be able to use this. So for the generic purposes this would not fit in.

Next I thought of modifying the alert template but then it would have an impact on all task lists which is not a recommended option.

So, I have decided that I have to disable the alerts for that Task list and use “SmtpClient or SPUtility” class instead to send email which can be used at different requirements such as “event handlers/workflows or any other for that matter. With this class we do have control over all the aspects “From, To, Subject, Email Body, etc”. The only problem with this to achieve the requirement # 2 (Ribbon control in outlook). I always wondered how would outlook recognize that an email is for Task, how is this ribbon control activated as soon as it sees an email.

I believed that the alert email sent has some headers which mark this as Task which is understood by outlook and displays the controls accordingly. So now the question is what are those headers. After some research I figured out the Mail message headers. Below is the code for sending an email which also includes those headers and satisfies all the above mentioned requirements (1, 2 & 3)

Any of the above mentioned solutions can be used based on the requirement. I have therefore listed out different approaches and I would leave it to your best judgement to decide on the selection of approach.

Above code is not a completely cleaned up code, you can remove unnecessary headers.

THanks venkat.. I have another doubt. without using the custom email body , with using the default mail notification , can we hide the open this task option. ? I have an issue, where after submitting the form from outlook, i get an error as :Invalid context for close the form action”

Have you created the workflow using SharePoint Designer? If yes, you can disable the assignment emails on the task list and have an additional action “Send an Email” to notify the users of item assignment.

Pardon my ignorance, but how exactly do you insert this code into the Send an Email action? I tried advanced properties and inserted it into the body, but that just emails me the text of the code, it doesn’t seem to render that way.

You’ll have to an event for each activity, inside that event you can paste the code (maybe just double click on the activity and it would take you to the appropriate code block). But if you are saying the text of the email is just a plain text and not the rich text then you’ll have to set the “IsBodyHtml” attribute to “true;” as below:

Hi Venkat,
Thanks for sharing this, exactly what I am after. However its not working 😦 below are the headers from the email but the Edit Task button is not visible in outlook. Do you see any problem with the headers? i have omited X-Sharing-Title, rest of them are all present.