Blog

Ramblings about development and woodworking

 

Inherit Sitecore insert options with this simple insert rule

Posted on in Development by Kyle Heon

No Comments
Praesent dictum diam

On my current project we have some global items stored in a series of folders. To make organization of these different items I created a structure that makes sense for the different types. We call these items cards and we have multiple types. We have a series of modules that consume these cards. This looks something like this:

  • Card Catalog
  • Call To Actions
  • Stories
  • Promotions
  • Statements

The insert options for these folders is simple, they allow for the creation of the appropriate card type. The problem with this setup is that no further folder structure can be made as the insert options would disappear. To solve this I created a simple insert rule that inherits options from the item. This insert rule can be added to any number of items and as long as an item has this rule assigned we inherit from the parent. Once we hit an item that does not the inheritance stops.

Here is the source code:

public class InheritParentInsertOptions : InsertRule
{
    private const string RuleId = "{1878D1F6-2F08-4F0F-96CE-0513D6903696}";

    public InheritParentInsertOptions(Int32 count)
    {
    }

    public override void Expand(List<Item> insertOptions, Item item)
    {
        while (null != item && ((MultilistField) item.Fields["__Insert Rules"]).Contains(RuleId))
        {
            MultilistField parentInsertOptions = item.Parent.Fields[FieldIDs.Branches];
            foreach (
                var insertOption in
                    parentInsertOptions.GetItems()
                                       .Where(
                                           insertOption => !insertOptions.Contains(insertOption, new ItemComparer()))
                )
            {
                insertOptions.Add(insertOption);
            }
            item = item.Parent;
        }
    }

    private class ItemComparer : IEqualityComparer<Item>
    {
        public bool Equals(Item x, Item y)
        {
            return (x.ID == y.ID);
        }

        public int GetHashCode(Item item)
        {
            return item.GetHashCode();
        }
    }
}

For our purposes I created a new Inheritable Folder that has this rule added automatically and converted all of our card storage folders to this new folder type. Insert Options for this folder allow this new folder to be added simplifying card organization. This might be overkill, given the nature/use of folders it might be okay to add this rule to the Common Folder template. I prefer to not modify system data templates though but at least wanted to point that out as an option (for what it's worth).

After I built and implemented this I decided to Google this subject to see if anyone else has blogged about this and John West details another approach that uses the Rules Engine to accomplish this. I encourage you to check out his option as well and pick the one that works best for you. If you've solved this in yet another way let me know in the comments below.

comments powered by Disqus

Categories

About

Dust Collected was founded in 2014 by Kyle Heon and is a play on a series of words that have meaning in both software engineering and woodworking.