Valid Item Names

Valid Item Names

Item Names

  • Certain web.config settings apply to all item names in the Content Tree
  • Use rules to allow different naming standards for certain items, such as metadata or media items
  • The InvalidItemNameChars setting will prevent Item names from being created if they contain any of the specified characters.  The default invalid characters are \/:?"<>|[]"/>
  • The ItemNameValidation setting will validate item names against the specified regular expression. The default expression is ^[\w\*\$][\w\s\-\$]*(\(\d{1,}\)){0,1}$
  • The MaxItemNameLength setting is used to control the length of all item names

DNS and Active Directory Names

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Settings
  • Sitecore.Data.Items.Item
  • System.String

Valid Item Names Configuration Settings

  • <configuration>
    • <sitecore>
      • <settings>
        • <setting name="InvalidItemNameChars" value="\/:?&quot;&lt;&gt;|[]"/>
        • <setting name="ItemNameValidation" value="^[\w\*\$][\w\s\-\$]*(\(\d{1,}\)){0,1}$"/>
        • <setting name="MaxItemNameLength" value="100"/>

Valid Item Names C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

char[] restrictedChars = Sitecore.Configuration.Settings.InvalidItemNameChars; 
System.String invalidChars = new System.String(restrictedChars);
string itemNameValidation = Sitecore.Configuration.Settings.ItemNameValidation;
string maxItemNameLength = Sitecore.Configuration.Settings.MaxItemNameLength.ToString();

output.Write("{0}&#58;&#160;{1}", "Invalid Item Name Characters", "<b>" + invalidChars + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Item Name Validation", "<b>" + itemNameValidation + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Max Item Name Length", "<b>" + maxItemNameLength + "</b>");

output.RenderEndTag(); //Div


Valid Item Names C# Web Control Output

Invalid Item Name Characters: \/:?"<>|[]
Item Name Validation: ^[\w\*\$][\w\s\-\$]*(\(\d{1,}\)){0,1}$
Max Item Name Length: 100

Sources