Sitecore Custom Workflow: Prevent Self Approval

·

2 min read

Creating a custom workflow in Sitecore is useful in a scenario where we need additional action to validate or do to some restriction on publishing.

In my case, I will need to restrict self approval from the same author.

Create a Custom Workflow Action

Create a new action item under the approve command in your workflow, based on the /sitecore/templates/System/Workflow/Action template.

image.png

Specify the assembly to our custom class in Type string field

Sitecore.Foundation.SitecoreExtensions.Workflow.Actions.RestrictApprover, Sitecore.Foundation.SitecoreExtensions

Create a simple class RestrictApprover

namespace Sitecore.Foundation.SitecoreExtensions.Workflow.Actions
{
    public class RestrictApprover
    {
        private const string awaitingApprovalStateID = "{53331E89-298E-4A0F-BE08-BB189EB2B1BD}";
        public void Process(Sitecore.Workflows.Simple.WorkflowPipelineArgs args)
        {
            var item = args.DataItem;
            var workflowProvider = item.Database.WorkflowProvider;

            if (workflowProvider.GetWorkflow(item) != null)
            {
                var workflow = workflowProvider.GetWorkflow(item);
                var history = workflow.GetHistory(item);
                var lastUpdated = history.Where(i => i.NewState == awaitingApprovalStateID).OrderByDescending(i => i.Date).FirstOrDefault();
                if (lastUpdated != null)
                {
                    if (string.Compare(lastUpdated.User, Sitecore.Context.User.Name, true) == 0)
                    {
                        Sitecore.Web.UI.Sheer.SheerResponse.Alert("You cannot approve your own changes.");

                        args.AbortPipeline();
                    }
                }
            }

            return;

        }
    }
}

The above custom workflow action will prevent the approval cannot be executed by the same person who last updated/submit the content/items for approval. An alert will be prompted to user who try to self-approve for their own changes

Additional step to make it perfect

Sitecore allows developers to dynamically control the name, logo, visibility on any workflow command. This allows us to change how commands appear in the UI based on custom code which can increase usability of implementations.

In my case, I would like to hide the approve command from the same person who submit the content for approval.

Create ValidateApproverEvaluator class and override EvaluateState

using Sitecore.Data.Items;
using Sitecore.Workflows;

namespace Sitecore.Foundation.SitecoreExtensions.Workflow.AppearanceEvaluators
{
    public class ValidateApproverEvaluator : BasicWorkflowCommandAppearanceEvaluator
    {
        private const string awaitingApprovalStateID = "{53331E89-298E-4A0F-BE08-BB189EB2B1BD}";
        public override WorkflowCommandState EvaluateState(Item item, Item workflowCommand)
        {
            var workflowProvider = item.Database.WorkflowProvider;

            if (workflowProvider.GetWorkflow(item) != null)
            {
                var workflow = workflowProvider.GetWorkflow(item);
                var history = workflow.GetHistory(item);
                var lastUpdated = history.Where(i => i.NewState == awaitingApprovalStateID).OrderByDescending(i=>i.Date).FirstOrDefault();
                if(lastUpdated != null)
                {
                    if (string.Compare(lastUpdated.User, Sitecore.Context.User.Name, true) == 0)
                    {
                        return WorkflowCommandState.Hidden;
                    }
                }
            }

            return base.EvaluateState(item, workflowCommand);

        }
    }
}

Last, specify the assembly to our custom class in Appearance Evaluator Type of your Approve command item

Sitecore.Foundation.SitecoreExtensions.Workflow.AppearanceEvaluators.ValidateApproverEvaluator, Sitecore.Foundation.SitecoreExtensions

image.png

Now, approve command is disappeared if an item is updated by the same user.