Home » Content Editor » Warn Content Authors on Having Too Many Sub-items Under an Item in Sitecore

Warn Content Authors on Having Too Many Sub-items Under an Item in Sitecore

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

In my previous post, I shared two field validators that will warn content authors/editors when they link to Items without presentation in Internal and General Link fields.

When I was building those two validators, I came up with another validator idea: how about warning content authors/editors when they have too many sub-items under an Item?

To accomplish this, I came up with the following class that serves as an Item validator:

using System;
using System.Runtime.Serialization;

using Sitecore.Buckets.Managers;
using Sitecore.Data.Items;
using Sitecore.Data.Validators;

namespace Sitecore.Sandbox.Data.Validators.ItemValidators
{
    [Serializable]
    public class ItemHasTooManySubitemsValidator : StandardValidator
    {
        public override string Name
        {
            get
            {
                return Parameters["Name"];
            }
        }

        private int MaxNumberOfSubitems
        {
            get
            {
                int maxNumberOfSubitems;
                if (!int.TryParse(Parameters["MaxNumberOfSubitems"], out maxNumberOfSubitems))
                {
                    return 0;
                }

                return maxNumberOfSubitems;
            }
        }

        public ItemHasTooManySubitemsValidator()
        {
        }

        public ItemHasTooManySubitemsValidator(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        protected override ValidatorResult Evaluate()
        {
            Item item = GetItem();
            if(IsValid(item))
            {
                return ValidatorResult.Valid;
            }

            Text = GetErrorMessage(item);
            return GetFailedResult(ValidatorResult.Suggestion);
        }

        protected virtual bool IsValid(Item item)
        {
            return MaxNumberOfSubitems < 1
                   || item == null 
                   || IsBucket(item)
                   || !item.HasChildren
                   || item.Children.Count <= MaxNumberOfSubitems;
        }

        protected virtual bool IsBucket(Item item)
        {
            if(item == null)
            {
                return false;
            }

            return BucketManager.IsBucket(item);
        }

        protected virtual string GetErrorMessage(Item item)
        {
            string message = Parameters["ErrorMessage"];
            if (string.IsNullOrWhiteSpace(message))
            {
                return string.Empty;
            }

            return GetText(message, new[] { item.DisplayName });
        }

        protected override ValidatorResult GetMaxValidatorResult()
        {
            return base.GetFailedResult(ValidatorResult.Suggestion);
        }
    }
}

The class above inherits from Sitecore.Data.Validators.StandardValidator in Sitecore.Kernel.dll — this is the base class which most validators in Sitecore inherit from — and ascertains whether the Item being validated has too many sub-items underneath it (the maximum number of allowed sub-items is passed to the class’ instance via the MaxNumberOfSubitems parameter set on the Validation Rule item — these have the /sitecore/templates/System/Validation/Validation Rule template — in Sitecore which is shown later in this post).

If the Item being validated has more sub-items than is allowed and isn’t an Item Bucket, the validator’s error message is set on the Text property of the class instance — the error message is passed via a parameter on the Validation Rule item — and a ValidatorResult instance is returned to the caller.

I then wired up the above class in Sitecore on a Validation Rule item, and set the maximum number of allowed sub-items to be four for testing (no, I’m not going to create a gazillion Items to test this):

create-validator-in-sitecore

Now that we have the Validation Rule Item in place, we should probably give content authors/editors the ability to remedy having too many sub-items under an Item.

How?

Let’s give them the ability to convert the Item into an Item Bucket. I created the following Menu item — this has the template of /sitecore/templates/System/Menus/Menu item — to empower content authors/editors on making this conversion:

covert-to-item-bucket-menu-item

I then had to set up my Sample Item template to be bucketable since we are giving the ability to bucket Items with this template:

sample-item-bucketable

I then mapped the Item validator to the Standard Values item of my Sample Item template:

item-validator-on-standard-values

For testing, I created some Items underneath another Item:

item-with-four-items

As you can see, we haven’t exceeded the maximum number of 4 quite yet.

I then created a fifth item, and was presented with a validation warning:

item-with-five-subitems

I right clicked on the square in the Validation Bar, and was presented with some options:

item-with-five-subitems-convert-to-bucket

I clicked on “Convert to Item Bucket”, and then saw a magical progress dialog followed by this:

item-is-now-a-bucket

If you have any thoughts on this, or ideas for other Item validators, please drop a comment.

Advertisement

3 Comments

  1. Love it. This should be in every Sitecore instance as part of setup. It’s going to be for me a least.

  2. Jason says:

    What is the practical limit for sub-items, say in version 7.2?

    • Though I don’t know of any limit — as far as I know there isn’t one — as a rule of thumb I tell people to avoid having more than 100 child Items under any Item though don’t have a good recommendation around all descendants for an Item.

      However, keep in mind, you will be capped at whatever the value is in /configuration/sitecore/settings/setting[@name=”Query.MaxItems”] when using a standard Sitecore query — this value “out of the box” is set at 100, and that would include all Items retrieved via a Sitecore query.

      If you find yourself having to put “tons” of Items — I know this is subjective but it also depends on what you are trying to achieve plus your particular requirements for these Items — that have the same data template, you might want to put them in an Item Bucket.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: