Workflow notifications in Sitecore (Roles & users)

Objective

To notify user(s) when an item moves into a certain state of an associated workflow in sitecore (Sitecore 6.5).

Introduction

Sitecore allows us to create workflows which can be associated with content items. In most scenarios, these workflows are a necessity seeing as any content seeking to appear in the live site, must undergo various steps of reviews and approval.

Sitecore lets you create a workflow with any number of states, each state can have a collection of commands, and each command can trigger a collection of actions.

Sitecore does provide multiple type of actions that can be added to execute when a command is triggered.

If you want to add an email action to notify users when an item is submitted / approved / rejected etc, you could sure try to use the out of the box email action (/sitecore/templates/System/Workflow/Email action). This works perfectly well when you have an email address as your recipient. When you have a role (all users in a role) or multiple roles or email addresses or a combination there of as your recipients, you will need to turn to a custom action (/sitecore/templates/System/Workflow/Action) for now.

Implementation

Add a custom action (/sitecore/templates/System/Workflow/Action) to the command which you want to add the notification functionality (Notify):

2014-04-26_013621

Available workflows are found at: /sitecore/system/Workflows.

A custom action takes a fully qualified class name, assembly name and optional parameters (available in the WorkflowPipelineArgs object)

A command (template: /sitecore/templates/System/Workflow/Command) has a Supress Comment field in addition to the Next State field. Left unchecked, the user is asked for a comment when this command is triggered. This comment appears in the item history:

2014-04-26_025107

And also note that this is available in the WorkflowPipelineArgs and is used in the highlighted line below.

Sample code of the custom action code:

using System.Configuration;
using System.Net.Mail;
using Mysite.Helpers;
using Sitecore.Workflows.Simple;

namespace Mysite.WorkflowActions
{
    public class NotifyReject
    {
        public void Process(WorkflowPipelineArgs args)
        {
            var contentItem = args.DataItem;

            var emailMessage = new MailMessage
            {
                From = new MailAddress(ConfigurationSettings.AppSettings["ContentSubmissionEmailFromId"]),
                Subject = "Content submitted: " + contentItem.Name,
                Body = "The following content item has been submitted for review: '" + contentItem.Name + "' (" + contentItem.Paths.Path + ")"
                       + " with the comment: '" + args.Comments + "'"
            };

            var userRolesSetting = ConfigurationSettings.AppSettings["ContentSubmissionEmailToRole"];
            emailMessage = WorkflowHelper.AddRecipientsToMail(emailMessage, userRolesSetting);

            WorkflowHelper.SendMail(emailMessage);
        }
    }
}

And here is the helper method which will allow you to take a configuration string of the like of ‘sitecore\Custom Content Authors|sitecore\Content Admin|admin@example.com’ and build a list of recipients to send out the email. In this snippet, the list of roles / email addresses has been stored in the web.config file. This could also be stored in a sitecore settings item, in which case it would be available to the content authors to update.

Note the highlighted lines to get the users from a role.


public static MailMessage AddRecipientsToMail(MailMessage mailMessage, string recipientsFromConfig)
        {
            var userRoleNames = recipientsFromConfig.Split('|');
            foreach (var userRoleName in userRoleNames)
            {

                if (Role.Exists(userRoleName))
                {
                    var role = Role.FromName(userRoleName);
                    var users = RolesInRolesManager.GetUsersInRole(role, true);
                    foreach (var user in users.Where(x => x.IsInRole(role)).Where(user => !string.IsNullOrEmpty(user.Profile.Email)))
                    {
                        mailMessage.To.Add(user.Profile.Email);
                    }
                }
                else
                {
                    var rgxEmail = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
                    if (rgxEmail.IsMatch(userRoleName))
                        mailMessage.To.Add(userRoleName);
                }
            }

            return mailMessage;
        }

You might also want to send out an email to the content author that last edited the item in question:

// Get user who last edited the item
var contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
var contentHistory = contentWorkflow.GetHistory(contentItem);

if (contentHistory.Length > 0)
{
	var lastUser = contentHistory.Last().User;
	var submittingUser = User.FromName(lastUser, false);
	emailMessage.To.Add(submittingUser.Profile.Email);
}
Advertisements

, , , , , , ,

  1. #1 by Ardelle Cumbaa on January 15, 2017 - 8:11 am

    you’re actually a good webmaster. The site loading speed is amazing. It seems that you are doing any unique trick. In addition, The contents are masterwork. you have done a fantastic process on this subject!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: