Note: This is a port of an old post from a previous blog, originally written to target Sitecore 6.3, though it still applies as of Sitecore 7.5. Whilst it is now not such a new subject, it may still prove useful to some.

Whilst a lot of content editors using Sitecore prefer to use the Page Editor, the Content Editor still sees a lot of use. One of the features of the content editor is the gutter validators, used to quickly provide a visual check of the validation state of items in the tree:

It’s very easy to add your own custom validation here.

Creating the code

First of all, create the code for your gutter validator. Create a new class and have it inherit from Sitecore.Shell.Applications.ContentEditor.Gutters.GutterRenderer. In this class you should override the GetIconDescriptor(Item item) method, which returns a GutterIconDescriptor.

This overriden method should return an instance of GutterIconDescriptor if the validation has failed, or null if the validation passes.

For the sake of example, here we have a validator that checks to see if an item has over 100 child items and if so will fail validation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class TooManyChildrenValidator : GutterRenderer
{
protected override GutterIconDescriptor GetIconDescriptor(Item item)
{
Assert.ArgumentNotNull(item, "item");
// If the item isn't valid, return the validation error description,
// otherwise return null.
return !ItemIsValid(item) ? GetGutterDescriptor() : null;
}
private bool ItemIsValid(Sitecore.Data.Items.Item item)
{
// Item passes validation if it has 100 children or less
return item.Children.Count() <= 100;
}
private static GutterIconDescriptor GetGutterDescriptor()
{
// Return a new descriptor showing a warning icon and message
GutterIconDescriptor descriptor = new GutterIconDescriptor();
descriptor.Icon = "Applications/16x16/information2.png";
descriptor.Tooltip = "This item has quite a lot of items underneath it and may affect Sitecore performance.";
return descriptor;
}
}

You can use the above code as a template, all you need to do is customize the ItemIsValid method with your own validation logic, and alter the GetGutterDescriptor method to choose an icon and tooltip that’s suitable for you.

That’s all there is too it for the code. Compile the assembly and make sure it is deployed to your Sitecore bin directory.

Creating the Validation Item

Now you need to create an item for the gutter validator in Sitecore, so that it can be selected. This should be located in the Core database underneath the item /sitecore/content/Applications/Content Editor/Gutters.

You should create your item using the /sitecore/templates/Sitecore Client/Content editor/Gutter Renderer template.

The Header field represents how your validator will appear in the context-menu, and the Type field is the fully-qualified name of your new type:

Trying it out

Once that’s saved you can jump back into the master database and enable it. Right-click in the gutter to enable your validator, and then you should see your validation icon wherever an item fails your validation: