Create and assign Insert rules

Current version: 10.0

As part of the process of assigning insert options, an Administrator can define insert rules, which dynamically redefine the effective insert options for a user at runtime. Developers can implement custom Insert rules for administrators to select.

Administrators can also implement Insert Options rules to define effective insert options, providing user interfaces to select rule parameters. Unlike insert rules that apply to specific items, insert options rules apply to all items, though conditions typically limit their application.

This topic describes how to create and assign an Insert rule.

Create an Insert rule

You can assign insert rules to an insert option to dynamically redefine the effective insert options that are available for a user at runtime.

To create an insert rule:

  1. Create a class that inherits from the Sitecore.Data.Masters.InsertRule class with a constructor that accepts a single System.Int32 parameter, and override the Expand() method. You can use the following code sample:

    RequestResponse
    namespace Namespace.Data.InsertRules
    {
      public class ClassName : Sitecore.Data.Masters.InsertRule
      {
       public ClassName(System.Int32 count)
      { 
    }
       public override void Expand( System.Collections.Generic.List<Sitecore.Data.Items.Item> insertOptions, Sitecore.Data.Items.Item item)
      { 
        //TODO: manipulate insertOptions 
      }
     } 
    }
  2. In the Content Editor, insert any required project-specific folders under /Sitecore/System/Settings/Insert Rules using the /Templates/Common/Folder data template template.

  3. In the relevant project-specific folder, insert an insert rule definition item using the /System/Branches/Insert Rule data template template.

  4. In the insert rule definition item, in the Data section, in the Type field, enter the signature of the class.

Note

To assign the insert rule to other items, see Assign or copy insert options.

Assign an insert rule

You can assign insert rules in the insert options themselves to dynamically redefine effective insert options for a user at runtime. You can also implement custom insert rules for administrators to select.

To assign an insert rule to all items based on a data template or to an individual item:

  1. In the Template Manager or the Content Editor, select the standard values item or individual item.

  2. On the Configure tab, in the Insert Options group, click Assign.

  3. In the Insert Options dialog box, on the Insert Rules tab, double-click the insert rule to move it to the Selected list.

  4. Click OK.

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

The following Insert rule example implements these requirements:

  • 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.

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

Do you have some feedback for us?

If you have suggestions for improving this article,