Easily Sanitize a Sitecore Item Name in C#
Say you’re creating a new Sitecore item programmatically in C#. The general approach is to take some piece of data from your original data point, e.g. an article in a feed, and sanitize it so your item name is valid in Sitecore. Let’s take the example of an article’s title. Most developers will strip out the invalid character’s they can think would cause issues in Sitecore, e.g. !,.:; etc…
Here’s an easier way to do it with Sitecore’s own configuration:
char[] invalidcharacters = Sitecore.Configuration.Settings.InvalidItemNameChars; string sanitizedName = string.Concat(possibleName.Trim().Split(invalidcharacters));
Notice Sitecore.Configuration.Settings.InvalidItemNameChars gives you a character array of the characters defined in the web.config. From there, we just trim whitespace away from the name, split the string at each “invalid” character and then concatenate the split chunks.
For re-usability, here it is as an overloaded extension method:
public static string SanitizeToItemName(this string possibleName)
{
return SanitizeToItemName(possibleName, Sitecore.Configuration.Settings.InvalidItemNameChars);
}
public static string SanitizeToItemName(this string possibleName, char[] invalidCharacters)
{
return string.Concat(possibleName.Trim().Split(invalidCharacters));
}
UPDATE:
Mark van Aalst identified that the Sitecore API has a utility method that does something like this already: Sitecore.Data.Items.ItemUtil.ProposeValidItemName(name, defaultValue);. Thanks for the tip Mark!
3 Comments + Add Comment
Got anything to say? Go ahead and leave a comment!
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under:
There is also a method called ProposeValidItemName within the Sitecore.Data.Items.ItemUtil class which does the same. It also has an overload for a default value.
string itemName = ItemUtil.ProposeValidItemName(name, defaultValue);
Thanks Mark! Didn’t know that existed — definitely going to be using that much more.
[...] to leverage the work they’ve done to ensure proper item names as pointed out in the comments here. So above all else, if you are trying to ensure that code you write is using the same logic as [...]