The Sitecore Gutter

No, this isn’t a sad article about unemployed Sitecore developers. And I’m still gainfully employed, thank you very much.

Instead, this article is about a Sitecore feature that a colleague pointed out to me recently. I hadnĀ“t noticed it before, but it seems pretty useful to me.

When you right-click in the bar left of the content tree, you’ll get a nice dropdown allowing you to select an option (or multiple options). It’s called the quick action bar, but I call it the gutter (I’ll explain later). There are some very useful options there that can give you a quick insight into what’s going on with your content items and you can also use it to manipulate the items.

For example, I’ve found the “Presentation Overriden” option to be quite useful. If selected, it displays an icon next to any item that does not have the standard presentation settings from the item’s template selected. This can prevent annoying problems, if you accidentaly changed the presentation settings for an individual item and you don’t see why the changes on the standard values seem to have no effect. You can click the icon to remove the overriden settings.

Anyway, this function gave me an idea: what if you could control the behaviour of the icons in the gutter dynamically, say, through the Sitecore rules engine? That would be pretty cool, because you would not have to do any coding to quickly see what items comply with a certain condition.

So I got to work, and quickly found the icons in the core database (/sitecore/content/Applications/Content Editor/Gutters). This is where I got the name: since the items are called GutterRenderers, it only seems logical to call it the gutter (or not… whatever).

I added a new item with the right template (/sitecore/templates/Sitecore Client/Content editor/Gutter Renderer) and set the values as displayed in the image below.

Please note that the datasource and fieldname point to an item’s field in the master database. That item’s field is a “Rules” field and can be used to set the condition.

All that was needed now was to implement the RuleBasedGutterRenderer.RuleBased class. Which is what I did:

 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.ContentEditor.Gutters;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Data.Fields;
using Sitecore.Rules;

namespace RuleBasedGutterRenderer
{
    public class RuleBased : GutterRenderer
    {
        protected override GutterIconDescriptor GetIconDescriptor(Item item)
        {
            Assert.ArgumentNotNull(item, "item");
            string datasource = Parameters["datasource"];
            string fieldName = Parameters["fieldname"];
            Assert.IsNotNull(datasource, "No datasource set on this rule based gutter renderer");
            Assert.IsNotNull(fieldName, "No field name set on this rule based gutter renderer");
            Item dsItem = item.Database.GetItem(datasource);

            Assert.IsNotNull(dsItem, string.Format("Datasource '{0}' is not valid", datasource));
            Assert.IsNotNull(dsItem.Fields[fieldName], string.Format("Item '{0}' does not have a field named '{1}'", item.ID, fieldName));

            RuleList<RuleContext> rules = RuleFactory.GetRules<RuleContext>(new Item[] { dsItem }, fieldName);
            RuleContext ctx = new RuleContext() { Item = item };
            RuleStack ruleStack = new RuleStack();
            foreach (Rule<RuleContext> rule in rules.Rules)
            {
                rule.Condition.Evaluate(ctx, ruleStack);
            }

            if ((bool)ruleStack.Pop())
            {
                GutterIconDescriptor descriptor = new GutterIconDescriptor();
                descriptor.Icon = dsItem.Appearance.Icon;
                descriptor.Tooltip = string.Format("This item matches the rules as configured in {0} (field={1}).", dsItem.Paths.FullPath, fieldName);
                return descriptor;
            }

            return null;
        }
    }
}

So now all you have to do is set a rule in that field and you are ready to go (be sure to clear the Sitecore cache and refresh the Sitecore desktop after changing the rule).

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.

UA-4216957-1