Start of Main Content

In my last post I introduced the Published Item Comparer and showed how convenient it was to visually compare an item between databases.  I also mentioned that the Published Item Comparer runs through a series of validations to determine differences between the item in context and the same item in the target database.   This post will explain how simple it is to create your own validators and remove existing validators.

Ready to kick off your next digital marketing project? Start the conversation.

We can’t wait to hear all about your organization’s unique needs and develop innovative ways you can foster lasting connections with your audiences.

Creating Your Own Validator

To create your own validator you will need to create a new class and inherit from the Validator base class (ItemComparer.Library.Validations.Validator). The base class contains the correct signature and parameters that the Item Comparer will call and pass during its validation process. I've included a quick example below which checks to make sure an item exists in the target database.

    public class ExampleValidator : Validator
    {
        public override List Validate(ItemComparerContext context)
        {
            //default list
            List outputs = new List();

            //check to see if the item exists in the target database which
            //was specified in the settings item
            Item publishedItem = context.TargetDatabase.GetItem(context.Item.ID);
            if (publishedItem == null)
            {
                outputs.Add("The item does not exist in the target database.");
            }

            return outputs;
        }
    }

Adding Your Validator to the Published Item Comparer

The Item Comparer's validators are managed within a configuration file. This gives us the flexibility to add, remove, and even position the order in which they run in. Positioning is important due to the fact that when the Item Comparer does its initial check against an item in the content tree, it stops when the first validation fails and outputs its result in the ribbon. Therefore its beneficial to have the less intensive, core validators run first. Once your Validator class is complete, you will need to add a line in the ItemComparer.config file (located: /App_Config/Include/ItemComparer.config). This will tell the Item Comparer to run the new validation during its process. If you needed to remove a Validator you only need to comment or remove that validation from the configuration file.

Item Comparer Context Object

Each validation class implements a member called Validate which takes in the ItemComparerContext object. This object gives you direct access to a few useful objects and was meant to decrease the amount of redundant lookups within each validator class.

    public class ItemComparerContext
    {
        public Item Item { get; set; }
        public ItemComparerSettingsItem ItemComparerSettingsItem { get; set; }
        public Database TargetDatabase { get; set; }
    }

Ending Note

Over the last couple months, Velir has added this module to several of our projects including a few of my own. My co-workers, clients and I are finding ourselves increasingly using this tool. One of the main reasons for the success of this module is we are able to add custom validations on a per project basis depending on that project's needs.

I look forward to any comments or feedback regarding the module and I hope it has made it a little easier to compare an item between databases. 

Published:

Latest Ideas

Take advantage of our expertise with your next project.