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!
2 Comments + Add Comment
Got anything to say? Go ahead and leave a comment!
Recent Comments
- Hendrik-Jan Randewijk on Scaling Sitecore Presentation Component Data Sources
- Professional-Info on How to Setup a Sitecore Preview Site to Review Content Before Publishing
- Sitecore Media Library Upload Errors « horizontalintegration on Storing Sitecore Media in the Database vs. the File System
- Hasham on Write to a Custom Sitecore Log with log4net
- Why I love Sitecore? « Sitecore basics! on Sitecore Admin Pages Explained
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
- 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
- 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.