SharePoint : Custom Task Email with Outlook ribbon control “Open this Task”

Hi Guys,

It was really interesting to work with my latest client. They had an very interesting and innovated idea to automate their manual file approval process.  The requirement was to create a multistage state machine workflow where users have privilege to approve and reject documents with some comments. It was very interesting to work with end client(as they them self don't know what they actually want).

I will not go deep into the workflow but surly will come back with another post explaining it. This post is more about a requirement which I faced during the development of the workflow. Just wanted to share the design of the Workflow:

WF

Requirement:  As a user, I should have receive an email from workflow with custom subject having document name embedded to the subject where I can directly do some action like approve/reject or comment on the document. I should see a button in email "Open this task" which will redirect me to the page where I can take actions.

1

It looks bit easy but have hidden challenges in it. 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 above figure. Let me rephrase the requirements:

  1. We have to customize the body and subject as per their request

  2. Ribbon button "Open this Task" should present when the email is sent


The above mentioned two points are two sides of the coin, which means if you are able to see one then second will not be visible to you. So the final solution's must have some trick to achieve both cases.

Solution: 

One point is clear that we can not go along with the default workflow email feature, we have to use "SmtpClient" or "SPUtility” to create email with custom body and subjects. Now question is how one can get ribbon button using SPUtility send email function.

  • 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)

/// <summary>
/// Method used to send the task created email instead of default SP WF email notification
/// </summary>
/// <param name="web"></param>
/// <param name="htmlBody"></param>
/// <param name="ToEmailId"></param>
/// <param name="listItem"></param>
/// <param name="emailSubject"></param>
/// <returns></returns>
public bool SendTaskMail(SPWeb web, string htmlBody, string toEmailId, SPList taskList, string itemId, string emailSubject)
{
try
{
string domain = web.Site.WebApplication.OutboundMailSenderAddress.Remove(0, web.Site.WebApplication.OutboundMailSenderAddress.LastIndexOf('@'));
var messageHeaders = new StringDictionary();
messageHeaders.Add("to", toEmailId);//author.User.Email);
messageHeaders.Add("subject", emailSubject);
messageHeaders.Add("content-type", "text/html");

messageHeaders.Add("Message-Id", "<3BD50098E401463AA228377848493927" + Guid.NewGuid().ToString("D") + domain + ">");
messageHeaders.Add("X-Sharing-Title", ConvertToBase64String("Body"));
messageHeaders.Add("X-AlertTitle", ConvertToBase64String("System"));
messageHeaders.Add("Content-Class", "MSWorkflowTask");
messageHeaders.Add("X-AlertWebUrl", ConvertToBase64String(web.Url));
messageHeaders.Add("X-AlertServerType", "STS");
messageHeaders.Add("X-AlertWebSoap", ConvertToBase64String(web.Url + "/_vti_bin/alerts.asmx"));
messageHeaders.Add("X-Sharing-Config-Url", "stssync://sts/?ver=1.1&type=tasks&cmd=add-folder&base-url=" + Uri.EscapeDataString(web.Url) + "&list-url=" + Uri.EscapeDataString(taskList.RootFolder.ServerRelativeUrl) + "&guid=" + Uri.EscapeDataString(taskList.ID.ToString("D")));
messageHeaders.Add("X-Sharing-Remote-Uid", taskList.ID.ToString("D"));
messageHeaders.Add("X-Sharing-WssBaseUrl", ConvertToBase64String(web.Url));
messageHeaders.Add("X-Sharing-ItemId", ConvertToBase64String(itemId));

SPUtility.SendEmail(web, messageHeaders, htmlBody);

return true;
}
catch (Exception ex)
{
Utilities.UpdateLogErr(ex, "Error while sending email. Please find details in audit logs");
return false;
}
}

Hope this helps!

Thank you for your time.

Happy SharePointing :) !!

Comments

Popular posts from this blog

Hide Ribbon on SharePoint 2013 using CSS

Get Comment Count in SharePoint

Configure external site as content sources in sharepoint search