As you may know, I spend a lot of my free time exploring Sitecore’s tools, utilities and the rest of its inner workings. I find it fun understanding how various pieces of Sitecore works. If you ask my wife about my love of everything Sitecore, she’d roll her eyes. Kathy would tell you she wishes I showed the same passion to learn how to safely and properly handle the various (deadly🛠) tools in our garage.
Think of me as Tim from Home Improvement. Tim knew more about tools than I and he also got injured more. I too have had many close calls, lil’blood loss, stitches and lots of bruises🤕. It’s important to understand what you’re working with; if you don’t, you could find yourself in a bind.
Learn everything you can about Sitecore, front and back, inside and out. While you are doing that, you should also get familiar with the basics like the Sitecore’s Utilities. You don’t have to use the code the Sitecore devs worked hard creating for us, but without knowing Sitecore’s most basic functionality, you could be missing out on learning something new and possibly cool.
I will show you three approaches that try to accomplish the basic task of making a friendly Sitecore item name. I will cover two different Sitecore Utility methods along with our trusty pal, Regex.
ItemUtil + StringUtil = A Url Friendly Item Name
These’re over a hundred ways to ensure your items end up with URL friendly names. The following examples are rudimentary.
Earlier today, I came across this simple regex expression that renames an item. It technically works, so that’s a plus, however, it’s slightly flawed.
Regex.Replace(wildItemName, @”[^0–9a–zA–Z]+“, ” “).Trim()
Result: strong Name of a Product sup sup Version 1 strong
As I mentioned, the flaw is slight. The regex failed to fully remove the HTML tag from the product name. I could tweak the regex to remove the HTML tags, but I want to use Sitecore’s utilities before I start messing with regex.
Result: strongName of a Productsupsup Version 1strong
Surprise😮! ItemUtil.ProposeValidItemName did a poor job compared to the slightly flawed regex expression from the first example. Unlike the ProposeValidItemName utility, the regex at least removed all the extra spaces between the words.
In the next example, I am going to combine two Sitecore Utility methods along with my own simple regex to provide “a good” option.
Regex + ItemUtil.ProposeValidItemName + StringUtil.RemoveTags
Result 1: Name of a Product Version 1
Result 2: name-of-a-product-version-1
The three worked together without causing a scene and overcame the challenge I laid before it. Bravo code example 👏🏻, you get a 🍪.
What I’d Choose in Real Life
The last example works great, however, I’m torn on how I feel about it. I may ask my mentor, hero and friend, Jon (a Regex King🤴🏼) to create the perfect regex expression that would strip out the HTML tags, remove all the special characters, never allow more than 1 space between words. I am positive Jon could finish my request in a couple minutes. Simplicity is key and just because I haven’t mastered regex doesn’t mean I don’t see it’s benefits.
Update 9/12/2017: I talked to Jon and Jon said he wouldn’t recommend using one regex expression to cover every requirement and said it’s ok to have a couple simpler expressions, if the requirements change simpler expressions would allow for easier modifications for regex novices like myself. With that advice, I decided that my last example is good enough.
I am going to open dotPeek, go crazy and dive deep into all the assemblies in search of an entertaining and insightful topic. Hopefully I can show you something new, hopefully I can avoid writing a post that’s similar to dozens of others. If not, I hope I can at the very least entertain🤡.
Thanks for reading!
Do you enjoy my oddly themed blogs and wish you had access to even more of me and my ideas? Good news, you’re in luck!
If 140 characters is your thing, follow me on Twitter.
If you hate reading and watching Sitecore videos entertains you, head over to my YouTube channel! Sometimes I entertain, sometime I provide useful Sitecore information and sometimes I can do both in the same video.
I can also be found hanging out on the Sitecore channels on Slack, I like it, although it occasionally triggers AOL chat room flashbacks from the olden days.