Example: Code to prevent multiple children based on a single data template

Abstract

How to prevent users from creating child items based on a data template.

The following Insert Rule example uses code to implement the requirements below:

  • Under the home item, users can create child items based on each of a number of different data templates.

  • Under the home item, users cannot create two child items based on any data template.

The following insert rule code meets these requirements:

namespace Namespace.Data.InsertRules
{
  public class PreventChildrenWithCommonTemplate : Sitecore.Data.Masters.InsertRule
  {
   public PreventChildrenWithCommonTemplate(System.Int32 count)
  { 
  }
  public override void Expand( System.Collections.Generic.List<Sitecore.Data.Items.Item> insertOptions, Sitecore.Data.Items.Item item)
  {
   foreach (Sitecore.Data.Items.Item insertOption in insertOptions.ToArray())
  {
     if(item.Axes.SelectSingleItem("./*[@@templateid='"+insertOption.ID + "']")!=null)
     {
      insertOptions.Remove(insertOption);
     }
    }
   }
  }
 }