Sitecore has out of the box workflow’s Email Notification functionality but one of the major part of workflow implementation is to send email to proper Sitecore role with current state and custom tokens.
We are sending email notification to sitecore role instead of single person because a role can have multiple persons who can approve the content depending on their availability.
As we know it is important for approver to know details about the item which is in workflow like Item name, Item path, Submitted by, Workflow URL. These all information we are getting with the help of custom tokens.
So we have added a new field for Sitecore role. Here’s how it looks:-
Once the item has been checked-in by content editors. Email will be triggered to all the users under Story Reviewer.
We have also created the custom token for letting the reviewer know who has submitted the item.
Here is the code for the same:-
public class WorkflowEmail { public void Process(WorkflowPipelineArgs args) { Assert.ArgumentNotNull(args, "args"); ProcessorItem processorItem = args.ProcessorItem; if (processorItem != null) { Item item = processorItem.InnerItem; string fullPath = item.Paths.FullPath; string from = GetValue(item, "from", args); string to = !string.IsNullOrEmpty(getToEmailList(item)) ? getToEmailList(item) : GetValue(item, "to", args); string host = GetValue(item, "mail server", args); string subject = GetValue(item, "subject", args); string body = GetValue(item, "message", args); var message = new MailMessage(); message.From = new MailAddress(from); message.To.Add(to); message.Subject = subject; message.Body = body; message.IsBodyHtml = true; new SmtpClient(host).Send(message); } } private string GetValue(Item commandItem, string field, WorkflowPipelineArgs args) { string text = commandItem[field]; if (text.Length <= 0) return String.Empty; return ReplaceString(text, args); } private string ReplaceString(string text, WorkflowPipelineArgs args) { Item workflowItem = args.DataItem; text = text.Replace("$itemName$", workflowItem.Name.ToString()); text = text.Replace("$itemSubmittedBy$", GetItemSubmitter(args)); text = text.Replace("$itemComments$", args.CommentFields["Comments"]); text = text.Replace("$itemPath$", workflowItem.Paths.FullPath); text = text.Replace("$itemLanguage$", workflowItem.Language.ToString()); text = text.Replace("$itemVersion$", workflowItem.Version.ToString()); text = text.Replace("$workboxUrl$", HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + "/sitecore/shell/Applications/Workbox.aspx?sc_bw=1"); bool itemHasLayout = !String.IsNullOrEmpty(workflowItem.Fields[FieldIDs.LayoutField].Value); if (itemHasLayout) text = text.Replace("$itemUrl$", HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + (Sitecore.Links.LinkManager.GetItemUrl(workflowItem))); else text = text.Replace("$itemPreviewUrl$", "This item is not a page and cannot be previewed."); return text ; } private string GetItemSubmitter(WorkflowPipelineArgs args) { string result = String.Empty; Item contentItem = args.DataItem; IWorkflow contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem); WorkflowEvent[] contentHistory = contentWorkflow.GetHistory(contentItem); if (contentHistory.Length > 0) { string lastUser = contentHistory[contentHistory.Length - 1].User; User user = User.FromName(lastUser, false); UserProfile userProfile = user.Profile; result = userProfile.FullName; } return result; } public string getToEmailList(Item innerItem) { string role = innerItem[Templates.WorkflowRoles.Fields.Roles]; if (!string.IsNullOrEmpty(role)) { var users = System.Web.Security.Roles.GetUsersInRole(role); StringBuilder userlist = new StringBuilder(); foreach (var user in users) { var membershipUser = System.Web.Security.Membership.GetUser(user); userlist.Append(membershipUser.Email); userlist.Append(","); } return userlist.ToString().Remove(userlist.Length - 1); } return string.Empty; } }
This will replace all the tokens and will also send email to all the users under specific roles.
How can I implement the code in a project in order to get email notification?