1
votes

We are storing Sitecore items programmatically via Sitecore API using a custom user interface.

Since Sitecore does not allow to have duplicate names, and Sitecore does not allow certain characters to be included in the item name we decided to store clock ticks as the item name. But this is not user friendly for Content editors. Also these URL's are not Search Engine Optimized (Since it's not human readable).

What is the recommended approach to solve this issue? How can we deal with duplicate names if we are going to store Item's "Title" as the item name?

1

1 Answers

6
votes

You should use DisplayName for storing the title and sanitized version of its as Name

string title = "Name with $tr@nge characters!";

// remove incorrect characters and add postfix if necessary to make it unique
string itemName = Sitecore.Data.Items.ItemUtil.GetUniqueName(parentItem, 
    Sitecore.Data.Items.ItemUtil.ProposeValidItemName(title));

Item item = parentItem.Add(itemName, template);

using (new EditContext(item))
{
    item.Fields[FieldIDs.DisplayName].Value = title;
}

enter image description here