Customizing Sitecore Workflow Emails | Digital Transformation
Digital Transformation Blog

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.

Subscribe to the Digital Transformation Weekly Digest

* indicates required

Leave a Reply

Perficient Digital Transformation Blog

Perspectives and insights from our thought leaders on change management, Big Data, cloud, mobile, integration, APIs, analytics, digital experience and business optimization strategies to drive digital transformation.

Archives