Example: Code to limit the number of child items under any item

Abstract

Code example that implements the requirement that users cannot create more than a specified number of child items under any item.

The following Insert Options pipeline processor code implements the requirement that users cannot create more than a specified number of child items under any item:

namespace Namespace.Pipelines.GetMasters
 {
 public class LimitChildren
 {
 private int _maxChildren = 25;
 public int MaxChildren
 {
 set
 {
 _maxChildren = value;
 } get { return _maxChildren; } } public void Process(Sitecore.Pipelines.GetMasters.GetMastersArgs args) { if(MaxChildren>-1 && args.Item.Children.Count>=MaxChildren) { args.Masters.Clear(); } } } }

Add the processor to the Insert Options pipeline definition in the web.config file after the existing Namespace.Pipelines.GetMasters.GetInsertRules processor but before the existing Namespace.Pipelines.GetMasters.CheckSecurity processor:

<uiGetMasters argsType="Sitecore.Pipelines.GetMasters.GetMastersArgs">
 <processor mode="on" type="Sitecore.Pipelines.GetMasters.GetItemMasters,Sitecore.Kernel"/>
     <processor mode="on" type="Sitecore.Pipelines.GetMasters.GetInsertRules, Sitecore.Kernel"/>
   <processor mode="on" type="Namespace.Pipelines.GetMasters.LimitChildren,Assembly"> 
   <maxChildren>10</maxChildren>
 </processor>
 <processor mode="on" type="Sitecore.Pipelines.GetMasters.CheckSecurity, Sitecore.Kernel"/> 
</uiGetMasters>