Skip to main content

Digital Transformation

Customizing Sitecore Workflow Emails

My colleague Candace recently wrote a guest post on Workflow Security in Sitecore, where she referenced the common desire of a Content Manager to have intelligent, context-specific emails generated around Sitecore Workflows.  Here’s her follow up on how to produce such emails.

Sitecore provides email functionality out of the box with its workflows.  It’s really easy to add.  Just add an Email Action to any command, fill in the standard Email fields and an email is on its way every time that command is performed.  Here’s how it looks:

Sitecore Workflow Email

That’s great, but you KNOW the first thing your business users will want is a customized email.  They’ll want to know what page was approved or rejected, not just that a page was approved.  They would like to see the comments that were entered as well.  This email functionality is not strictly necessary, of course, because the Workbox provides the ability to see all workflow items, but an email is more immediate and convenient.

So it’s time to write custom email classes that provide much more flexibility.  Examples of these would include an ApprovalEmail, so that a submitting user knows that her content has made it to production.  More important is a class that notifies a submitter when a page has been rejected, so that the user can fix it up before submitting it again.  And an approver may want to be notified when another item lands in her approval queue.  Once these classes are written, they can be attached to the generic Sitecore Workflow Action, with the class and assembly entered as follows in the Type String:

Workflow Email Rejection

Let’s map out our needs for a Rejection Email:

  • We need to know which user submitted the item so we can notify him that his item is approved
  • We’ll want to send the email “from” the approver
  • The email message should include the page that was rejected and the reason it was rejected.

To get the user who submitted the item, the previous item in the Workflow history is retrieved because that item recorded the submitter’s information.  This code shows how to retrieve the history and the user’s email address:

Note: args are passed into this function as follows: Process(WorkflowPipelineArgs args)

     var contentItem = args.DataItem;
     var contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
     var contentHistory = contentWorkflow.GetHistory(contentItem); //this is the Content History array

     if (contentHistory.Length > 0)
     {
         //get submitting user’s email
         string lastUser = contentHistory[contentHistory.Length - 1].User;
         var submittingUser = Sitecore.Security.Accounts.User.FromName(lastUser, false);
         string useremail = submittingUser.Profile.Email; //original submitter
     }

An email can now be formatted.  The items to include in the email body are the item name and path.  We already have the contentItem in the code above.  We can now retrieve the contentItem.DisplayName and contentItem.Paths.Path.  The comments entered are available as args.Comments.  This email will be much more informative than the standard out-of-the-box one.

There are various ways to determine the approver(s) the email should go to.  One method would be to set up a shared account in Outlook that the approver(s) have access to and simply send the email there.  Another method would be to get all the approvers in a given role.  The method our users seem to prefer is to put an Approver’s email address in a Lookup Item in Sitecore.  We have a Lookup section in our content tree, and one of its folders is now EmailAdresses.  Approver Email goes under that folder.  Emails are submitted to that address and Approval/Rejection emails come from that address.  If the address needs to be changed temporarily or permanently, it’s a 2-second change.

So with just a bit of code and configuration, useful emails are generated and users are more productive.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Jamie Stump

My name is Jamie Stump, and I am a Senior Sitecore Consultant at Perficient. I was honored to be named one of only 42 2013 Sitecore MVP’s worldwide. I specialize in Sitecore Architecture and Development and my broad Sitecore experience includes Sitecore installation, configuration and CEP development, including custom DMS implementations for clients. I have implemented Sitecore solutions for a number of industry verticals including manufacturing, healthcare, financial services, advertising and retail. In addition to architecting and implementing Sitecore sites and eCommerce solutions, I also work with other Microsoft Technologies, including the .NET platform and SQL Server. You can read through my older Sitecore related blog posts here and my newer ones here. I graduated with a Bachelor of Science in Information Systems Development from York College of PA. I am originally from the suburbs of Philadelphia, PA, and still reside there with my wife, son, English bulldog and 2 cats.

More from this Author

Follow Us