3

Lets say the user itself is able to add new branch templates.

And on a homepage item, insert options must consist of items inside that branch templates folder.

In sitecore, insert options can only be set to specific items. When I select a folder as an insert option, sitecore shows that folder item (which is perfectly normal).

I need to make something like either showing items dynamically inside a specific folder, or setting the starting path of insert options browsing dialog.

Is any of this possible ?

Batu
  • 431
  • 1
  • 6
  • 17

2 Answers2

5

Blog post: https://sitecorealekseyshevchenko.wordpress.com/2017/09/19/dynamic-insert-options/

Create 'Dynamic Insert Option' template wich contains the only field 'Starting Path' type of 'Droptree' and source value is '{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 which should have dynamic insert options.

enter image description here

Patch 'uiGetMasters' processor with such config:

<?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); }
            }
        }
    }
}

The result see on pic below:

enter image description here

Aleksey Shevchenko
  • 1,159
  • 1
  • 11
  • 23
  • 1
    It worked like a charm, thank you. But the line `foreach (var master in args.Masters) { args.Masters.Remove(master); }` threw an error `collection is modified` so I replaced it with a for loop `for (int i = args.Masters.Count - 1; i > -1; i--) { args.Masters.RemoveAt(i); }` – Batu Sep 19 '17 at 11:06
  • Thanks for including the info in the answer, the blog post link appears to be dead. – Davy8 Jan 24 '19 at 17:01
0

You can use Sitecore Insert Options Rules instead of Sitecore Insert Options.

Find /sitecore/system/Settings/Rules/Insert Options/Rules item and create new item under that node using Insert Options Rule template. Now edit the rule and set it to:

where the item is the Home item or one of its descendants
    and where the parent template is Folder
add YOUR_TEMPLATE insert option

There are plenty of other conditions you can use and the rule can be much more complex if you requirements demands it.

You can read more here: Sitecore Insert Options Rules

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
  • this has nothing to do with my problem. the issue is that "add YOUR_TEMPLATE insert option" must be set statically to a specific object, i'm looking for a dynamic solution. – Batu Sep 19 '17 at 09:38
  • I think I haven't understood your requirements correctly. Can you maybe try to describe it in more extended way or give some example of what you need? – Marek Musielak Sep 19 '17 at 10:02
  • instead of setting static items as insert options, i want to assign a folder and display the child items under that folder as insert options. because there will be additions and deletions, the insert options will have to change dynamically. – Batu Sep 19 '17 at 10:09
  • @Batu check my solution. It gives ability to choose a folder and display the children under that folder as insert options. – Aleksey Shevchenko Sep 19 '17 at 10:18
  • @AlekseyShevchenko yeah i saw your solution is directly on point, applying it right now. – Batu Sep 19 '17 at 10:24
  • @Batu pretty good. if it is suited for you please apply my answer as right. – Aleksey Shevchenko Sep 19 '17 at 10:41