Dynamic Insert Options

This post provides solution which gives ability to choose a folder and display the children under that folder as insert options.

Create template, in my case, it is ‘Dynamic Insert Option‘ one, which contains ‘Starting Path‘ droptree field, set source to ‘{3C1715FE-6A13-4FCF-845F-DE308BA9741D}‘ (id of ‘/sitecore/templates‘ item).

Then add ‘Dynamic Insert Option‘ template to list of templates in ‘Base template‘ field of the template (‘Custom Folder‘ in me case) which should have dynamic insert options.

dynamic-insert-options-1

Patch uiGetMasters processor:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <processors>
      <uiGetMasters>
        <processor mode="on" type="DynamicInsertOption.Processors.GetDynamicInsertOption, DynamicInsertOption" patch:before="processor[@type='Sitecore.Pipelines.GetMasters.CheckSecurity, Sitecore.Kernel']" />
      </uiGetMasters>
    </processors>
  </sitecore>
</configuration>

Implement GetDynamicInsertOption processor:

namespace DynamicInsertOption.Processors
{
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.Pipelines.GetMasters;

    public class GetDynamicInsertOption
    {
        public void Process(GetMastersArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            var startingPath = args.Item["Starting Path"];

            if (!string.IsNullOrEmpty(startingPath))
            {
                for (int i = args.Masters.Count - 1; i > -1; i--) { args.Masters.RemoveAt(i); }

                var startingFolder = args.Item.Database.GetItem(startingPath);

                foreach (Item master in startingFolder.Children) { args.Masters.Add(master); }
            }
        }
    }
}

Solution in action:

dynamic-insert-options-in-action

 

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s