Set up custom workflow notifications

When using workflows in your content editing process, users in various roles need to work with items at different stages of the content life cycle. Notifications about workflow step transitions can help people keep track of their work. For example, users may wish to be notified when an item transitions to a step that requires their approval or review.

Developers can set up notifications by handling global events that the system triggers when items transition between workflow steps:

  • MoveToStep – triggered when a content item, page, or headless item is moved from one workflow step to another. Handled via the ContentItemWorkflowEvents, WebPageWorkflowEvents or HeadlessItemWorkflowEvents classes.
  • Publish – triggered when a content item, page or headless item finishes its workflow cycle and is published. Handled via the ContentItemEvents, WebPageEvents or HeadlessItemEvents classes.
  • UpdateLanguageMetadata – triggered when a reusable content item or page is scheduled to be published or unpublished, or when a scheduled publish action is canceled. Handled via the ContentItemEvents or WebPageEvents classes.

The content of the notifications can either be created directly in the code, or you can use a custom notification and then manage the content in the Xperience administration.

Limitations:

  • The system currently does not provide events covering workflow transitions of emails.
  • Workflow events are not triggered when an item enters the system’s default Draft step for the first time in a workflow cycle, for example after creating a completely new item or a new version of a published or unpublished item. For newly added items, you can handle the Create event via the ContentItemEvents, WebPageEvents or HeadlessItemEvents classes.

Example

Prerequisites

To integrate the code of this example, prepare a custom Class Library project in your solution.

To allow the system to send out emails, you also need to set up and configure an email client (for example an SMTP server or SendGrid integration). For more information, see Email configuration.

This example demonstrates how to set up basic email notifications informing about workflow step transitions. The example sends notifications when a reusable content item or website channel page is moved to a different step in any workflow. The recipients of the emails depend on the step:

  • Custom workflow steps – sent to users who are allowed to work with the item in the given step, i.e. all users belonging to the roles assigned to the step.
  • Draft (system step)Draft is the default first step in all workflows, and cannot have any roles assigned. The notification emails are instead sent to users with roles that have full control for the workflow, and to users with the ContentEditor role (as an example). Note that notifications are not sent when an item enters the Draft step for the first time in a workflow cycle, for example after creating a completely new item or a new version of a published or unpublished item.

For the sake of simplicity, this example does not send notifications when pages or content items are published. However, you can set up publish notifications using a similar approach by handling the ContentItemEvents.Publish and WebPageItemEvents.Publish events.

You can expand or simplify the implementation based on your project’s content types and workflow steps, as well as the requirements of your content editors. If you wish to use a different type of notifications than emails, replace the email API calls in the example’s code (e.g., with an external messaging SDK).

To view the example’s code, download the following files or see the code blocks below:

C#
Module class and handler assignment

using CMS;
using CMS.DataEngine;
using CMS.ContentWorkflowEngine;
using CMS.Core;
using CMS.EmailEngine;
using CMS.Membership;
using CMS.Websites;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(Custom.CustomWorkflowModule))]

namespace Custom
{
    public class CustomWorkflowModule : Module
    {
        private IEmailService emailService;
        private IInfoProvider<ContentWorkflowStepInfo> workflowStepProvider;
        private IInfoProvider<ContentWorkflowStepRoleInfo> workflowStepRoleProvider;
        private IInfoProvider<ContentWorkflowRoleInfo> workflowRoleProvider;
        private IInfoProvider<WebsiteChannelInfo> websiteChannelInfoProvider;
        private IRoleInfoProvider roleInfoProvider;
        private IUserInfoProvider userInfoProvider;
        private IUserRoleInfoProvider userRoleInfoProvider;
        private IOptionsMonitor<SystemEmailOptions> systemEmailOptions;

        // Module class constructor, the system registers the module under the name "CustomWorkflow"
        public CustomWorkflowModule()
            : base(nameof(CustomWorkflowModule))
        {
        }

        // Contains initialization code that is executed when the application starts
        protected override void OnInit(ModuleInitParameters parameters)
        {
            base.OnInit();

            // Gets instances of required services
            emailService = parameters.Services.GetRequiredService<IEmailService>();
            workflowStepProvider = parameters.Services.GetRequiredService<IInfoProvider<ContentWorkflowStepInfo>>();
            workflowStepRoleProvider = parameters.Services.GetRequiredService<IInfoProvider<ContentWorkflowStepRoleInfo>>();
            workflowRoleProvider = parameters.Services.GetRequiredService<IInfoProvider<ContentWorkflowRoleInfo>>();
            websiteChannelInfoProvider = parameters.Services.GetRequiredService<IInfoProvider<WebsiteChannelInfo>>();
            roleInfoProvider = parameters.Services.GetRequiredService<IRoleInfoProvider>();
            userInfoProvider = parameters.Services.GetRequiredService<IUserInfoProvider>();
            userRoleInfoProvider = parameters.Services.GetRequiredService<IUserRoleInfoProvider>();
            systemEmailOptions = parameters.Services.GetRequiredService<IOptionsMonitor<SystemEmailOptions>>();

            // Assigns a handler to the MoveToStep workflow event for reusable content items
            ContentItemWorkflowEvents.MoveToStep.Execute += ContentItem_MoveToStepEventHandler;

            // Assigns a handler to the MoveToStep workflow event for website channel pages
            WebPageWorkflowEvents.MoveToStep.Execute += WebPage_MoveToStepEventHandler;
        }

        // The event handlers and other helper methods can be added here
        // ...
    }
}
C#
Email data class

// Contains properties for passing data to workflow notification email messages
public class EmailData
{
    public string CurrentStepDisplayName { get; set; }
    public string OriginalStepDisplayName { get; set; }
    public string StepChangedByUserName { get; set; }
    public string ItemName { get; set; }
    public string Recipients { get; set; }
}
C#
Content item MoveToStep handler

// Handler for the MoveToStep event for content items
// Sends an email notification to all users who can work with the item in the current step
private void ContentItem_MoveToStepEventHandler(object sender, ContentItemWorkflowMoveToStepArguments e)
{
    ContentWorkflowStepInfo currentWorkflowStep = workflowStepProvider.Get(e.StepName);

    string recipients;
    
    // If the current step is the 'Draft' system step, gets the email addresses of all users
    // belonging to specific roles, or roles that can work with all steps in the workflow.
    if (currentWorkflowStep.ContentWorkflowStepType == ContentWorkflowStepType.SystemDraft)
    {
        recipients = GetRecipientsForUserIds(GetUserIdsForEditors(currentWorkflowStep.ContentWorkflowStepWorkflowID));
    }
    // For custom workflow steps, gets the email addresses of all users belonging to roles
    // who can work with the given workflow step.
    else
    {
        recipients = GetRecipientsForUserIds(GetUserIdsForWorkflowStep(currentWorkflowStep.ContentWorkflowStepID, currentWorkflowStep.ContentWorkflowStepWorkflowID));
    }

    // Prepares data for the email message
    EmailData emailData = new EmailData
    {
        CurrentStepDisplayName = currentWorkflowStep.ContentWorkflowStepDisplayName,
        OriginalStepDisplayName = workflowStepProvider.Get(e.OriginalStepName).ContentWorkflowStepDisplayName,
        StepChangedByUserName = userInfoProvider.Get(e.UserID).UserName,
        ItemName = e.DisplayName,
        Recipients = recipients
    };

    SendWorkflowNotificationEmail(emailData);
}
C#
Page MoveToStep handler

// Handler for the MoveToStep event for website channel pages
// Sends an email notification to all users who can work with the page in the current step
private void WebPage_MoveToStepEventHandler(object sender, WebPageWorkflowMoveToStepArguments e)
{
    ContentWorkflowStepInfo currentWorkflowStep = workflowStepProvider.Get(e.StepName);            
    
    string recipients;

    // If the current step is the 'Draft' system step, gets the email addresses of all users
    // belonging to specific roles, or roles that can work with all steps in the workflow.
    if (currentWorkflowStep.ContentWorkflowStepType == ContentWorkflowStepType.SystemDraft)
    {
        recipients = GetRecipientsForUserIds(GetUserIdsForEditors(currentWorkflowStep.ContentWorkflowStepWorkflowID));
    }
    else
    {
        // For custom workflow steps, gets the email addresses of all users belonging to roles
        // who can work with the given workflow step.
        recipients = GetRecipientsForUserIds(GetUserIdsForWorkflowStep(currentWorkflowStep.ContentWorkflowStepID, currentWorkflowStep.ContentWorkflowStepWorkflowID));
    }

    // Prepares data for the email message
    EmailData emailData = new EmailData
    {
        CurrentStepDisplayName = currentWorkflowStep.ContentWorkflowStepDisplayName,
        OriginalStepDisplayName = workflowStepProvider.Get(e.OriginalStepName).ContentWorkflowStepDisplayName,
        StepChangedByUserName = userInfoProvider.Get(e.UserID).UserName,
        ItemName = e.DisplayName,
        Recipients = recipients
    };

    SendWorkflowNotificationEmail(emailData);
}
C#
Helper methods

// Gets the IDs of all users belonging to a role that can work with a specified workflow and step
private IEnumerable<int> GetUserIdsForWorkflowStep(int stepId, int workflowId)
{
    // Gets the IDs of roles assigned to the workflow step
    var stepRoleIds = workflowStepRoleProvider
                 .Get()
                 .WhereEquals(nameof(ContentWorkflowStepRoleInfo.ContentWorkflowStepRoleContentWorkflowStepID), stepId)
                 .Column(nameof(ContentWorkflowStepRoleInfo.ContentWorkflowStepRoleRoleID))
                 .GetListResult<int>();

    // Adds the IDs of roles that are allowed to work with all steps in the workflow
    var roleIds = stepRoleIds.Union(GetRoleIdsWithFullControlForWorkflow(workflowId));

    // Gets the IDs of users belonging to the roles (without duplicates)
    var userIds = new HashSet<int>();
    foreach (int roleId in roleIds)
    {
        var userIdsForRole = userRoleInfoProvider
                 .Get()
                 .WhereEquals(nameof(UserRoleInfo.RoleID), roleId)
                 .Column(nameof(UserRoleInfo.UserID))
                 .GetListResult<int>();

        userIds.UnionWith(userIdsForRole);
    }

    return userIds;
}

// Gets the IDs of all users belonging to the 'ContentEditor' or 'Administrator' roles,
// or roles that are allowed to work with all steps in the workflow.
private IEnumerable<int> GetUserIdsForEditors(int workflowId)
{
    var roleIds = new List<int>
    {
        // Adds the system 'administrator' role, which can automatically work with all applications
        roleInfoProvider.Get(RoleName.ADMINISTRATOR).RoleID
    };

    // Adds the role with the 'ContentEditor' code name. You can adjust this implementation to load a list of any required roles.
    RoleInfo contentEditorRole = roleInfoProvider.Get("ContentEditor");
    if (contentEditorRole is not null)
    {
        roleIds.Add(contentEditorRole.RoleID);
    }

    // Adds the IDs of roles that are allowed to work with all steps in the workflow
    roleIds = roleIds.Union(GetRoleIdsWithFullControlForWorkflow(workflowId)).ToList();

    // Gets the IDs of users belonging to the roles (without duplicates)
    var userIds = new HashSet<int>();
    foreach (int roleId in roleIds)
    {
        var userIdsForRole = userRoleInfoProvider
                 .Get()
                 .WhereEquals(nameof(UserRoleInfo.RoleID), roleId)
                 .Column(nameof(UserRoleInfo.UserID))
                 .GetListResult<int>();

        userIds.UnionWith(userIdsForRole);
    }

    return userIds;
}

// Gets the IDs of roles that are allowed to work with all steps in the specified workflow
private IList<int> GetRoleIdsWithFullControlForWorkflow(int workflowId)
{
    return workflowRoleProvider
                            .Get()
                            .WhereEquals(nameof(ContentWorkflowRoleInfo.ContentWorkflowRoleContentWorkflowID), workflowId)
                            .Column(nameof(ContentWorkflowRoleInfo.ContentWorkflowRoleRoleID))
                            .GetListResult<int>();
}

// Converts a collection of user IDs to a recipient string containing the users' email addresses, separated by semicolons
private string GetRecipientsForUserIds(IEnumerable<int> userIds)
{
    var userEmails = userIds.Select(userId => userInfoProvider.Get(userId).Email);

    return String.Join(";", userEmails);
}
C#
Email sending logic

private void SendWorkflowNotificationEmail(EmailData emailData)
{
    // Only sends the notification email if the recipient string is not empty
    if (string.IsNullOrEmpty(emailData.Recipients))
    {
        return;
    }

    // Creates the email message object
    EmailMessage msg = new EmailMessage()
    {                
        From = $"no-reply@{systemEmailOptions.CurrentValue.SendingDomain}",
        
        // Sets the 'To' email address
        // Separate addresses with a semicolon ';' to add multiple recipients
        Recipients = emailData.Recipients,
        
        Priority = EmailPriorityEnum.Normal,
        
        // Sets the subject line of the email
        Subject = $"Item '{emailData.ItemName}' moved to step '{emailData.CurrentStepDisplayName}'",

        // Sets the body of the email
        Body = $"The '{emailData.ItemName}' item was moved from step '{emailData.OriginalStepDisplayName}' to '{emailData.CurrentStepDisplayName}' by user '{emailData.StepChangedByUserName}'.",
    };

    // Adds the email message to the email queue
    // The email is then sent using a configured email client (e.g., an SMTP server or SendGrid)
    emailService.SendEmail(msg);
}