Long time and no post but I’m hoping to get back into the swing of things.

Decided to start off with something straightforward but hopefully useful, it’s a workflow action that will triage items that fail validation into another state. This would help content editors who lack technical skills and would otherwise have to fix XHTML validation errors themselves.

I’ve included both the code below and a prefab package, should be as simple as installing, adding the action to your workflow command and pointing it to the awaiting validation state, give me a shout if you have any trouble with it.

using System.Threading;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Data.Validators;
using Sitecore.Diagnostics;
using Sitecore.Web.UI.Sheer;
using Sitecore.Workflows.Simple;
namespace WorkflowActionLibrary
{
    public class TriageAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            var workflowItem = args.DataItem;
            var validators = ValidatorManager.BuildValidators(ValidatorsMode.Workflow, workflowItem);
            var options = new ValidatorOptions(true);
            ValidatorManager.Validate(validators, options);
            ValidatorResult validatorResult = Validate(validators, args);
            if (validatorResult != ValidatorResult.Valid)
            {
                Item processorItem = args.ProcessorItem.InnerItem;
                string triageState = processorItem.Fields["TriageState"].Value;
                if(!string.IsNullOrEmpty(triageState))
                {
                    workflowItem.Editing.BeginEdit();
                    workflowItem.Fields[FieldIDs.WorkflowState].Value = triageState;
                    workflowItem.Editing.EndEdit();
                    args.AbortPipeline();
                }
            }
        }
        private static ValidatorResult Validate(ValidatorCollection validators, WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(validators, "validators");
            Assert.ArgumentNotNull(args, "args");
            ProcessorItem processorItem = args.ProcessorItem;
            if (processorItem == null)
            {
                return ValidatorResult.Valid;
            }
            const int @timout = 0x2710;
            const int timoutIncrement = 500;
            int counter = 0;
            while (true)
            {
                ValidatorResult validatorResult = ValidatorResult.Valid;
                foreach (BaseValidator validator in validators)
                {
                    if (validator.IsEvaluating)
                    {
                        validatorResult = ValidatorResult.Unknown;
                        break;
                    }
                    if (validator.Result > validatorResult)
                    {
                        validatorResult = validator.Result;
                    }
                }
                if (validatorResult != ValidatorResult.Unknown)
                {
                    return validatorResult;
                }
                counter++;
                if ((counter * timoutIncrement) > timout)
                {
                    SheerResponse.Alert("Timeout on item validation during Triage Workflow action.");
                    args.AbortPipeline();
                    return ValidatorResult.Unknown;
                }
                Thread.Sleep(timoutIncrement);
                ValidatorManager.UpdateValidators(validators);
            }
        }
    }
}