Friday, May 9, 2014

Sitecore Tips: Renaming Items

Renaming an item is a very straightforward process, right?  Right click on an item in the content tree, click rename, and a dialog appears asking for a new name.  It cannot get simpler than that.

Invalid Names

Right out of the box, Sitecore performs validations on the new item name you choose.  Sitecore does not like invalid characters and makes sure you do not use them or it prompts you for another name.  Can you get around this?  Of course you can, this is Sitecore after all.

If you inspect the web.config file under UI pipelines, you will see this section:



This is the UI pipeline with all the processes that take place when a user renames an item.  Keep in mind that adding a new item also calls this pipeline when you are prompted to enter a name for the new item.  If you use a decompiler and inspect the code for all the processors and methods called, you will see that the method that does the name validation is in "GetNewName" which calls:



This indicates that the item name is validated via a regex call and it is also stored as a setting key-value pair in the web.config file:



For this example we will modify the regex expression to match names that contain a literal dot or period:



Save and reload Sitecore and try renaming a new item to be "www.yahoo.com" and you will see the error message no longer appears and items with dots in the name will pass all validations.

Renaming Item Name Programmatically

Using the above example, we can ensure the item name of our choice passes checks and gets accepted by Sitecore.  But this still doesn't address the problem of using bad names.  If you create a new item under home with the name "www.yahoo.com", the item path for this new item would be "/sitecore/content/home/www.yahoo.com".

If this is not ideal and you rather convert the item name to be something else, such as replacing the dots with hyphens, while keeping the display name intact you can add some post processing to the item name.  To do this you must add an event handler to the "item:saved" event:



Add a new handler at the end to handle renaming the item.  Of course, it would be best to isolate this new handler to an external config file in the includes section.  Use a decompiler to inspect the other handlers to understand how to create a new handler if you need assistance in this area.

Basically, you want to do some name modifications.  With our example, we want to replace all white spaces and dots with dashes.  Follow these basic steps:

item.Editing.BeginEdit();
string processedName = ItemUtil.ProposeValidItemName(item.Name.ToLower().Replace(' ', '-').Replace('.', '-'));
item.Appearance.DisplayName = item.Name;
item.Name = processedName;
item.Editing.EndEdit();

Of course, you would have to put in your try/catches and other validations too but the above should get you started.


Summary

This illustrates how powerful Sitecore is for developers and how customizable everything is.  Add a new entry in a config file, create a new class with some methods, and change the way things work to customize to your specific needs.

No comments:

Post a Comment