Knowledge Base

Working With Custom Hooks: OnRename

The OnRename custom hook is triggered when a page, component, or folder is renamed in the Site Tree. Follow along as we demonstrate how to customize and extend this functionality.


This custom hook is triggered when a page, component, or folder is renamed in the Site Tree. It’s not too often that you would need to trigger more actions naturally based on someone changing the name of something, unless you are trying to keep to naming conventions and you are either stopping them from breaking those conventions or you are trying to make sure the names of other things in the system stay current with the new name.

On the other hand, these hooks can be used as a sort of testing point or easy trigger for an action you want to have happen. Renaming a page is quite easy in the CMS and you have the full name of the page as input to play with. You can use that to your advantage when testing a new script that you want to build that would normally require a more complicated trigger and then move the code once you are done testing it out.

Considerations

Avoid changing the name of a page (or component) within OnRename to prevent infinite loops and make sure you are carefully distinguishing between the new name and the old name.

Examples

Taxonomy Category Renaming

This example is meant to keep a category name in sync with the name of a page. Consider that you are using that category to tag content, such as news and events, so that it all collects into a department front page with a taxonomy navigation. If that department changed names, you would want the category to match, and it could be difficult to keep track of each place in the CMS where you used the name. The script could be further expanded to rename asset folders, groups in administration, or anything else in the CMS – or outside the CMS that the CMS can access.

OnRename Custom Hook for Taxonomy Category Renaming

First, pick the main part of the event to test on: you only want this to happen if the page schema was DepartmentPage. Have in place a hidden text field on the page schema to store the matching category ID instead of matching the category on name because someone might have manually changed the name or added a second category with the same name.  We want to make sure we are getting the correct category. You can then get into the Taxonomy Manager which contains all the objects for that system and use it to get the category that matches the ID in the text field, testing along the way to make sure you don’t end up with nulls (anything could have been deleted) before moving on to finally renaming the category.

if (page.SchemaName == "DepartmentPage")

{

    IElement categoryIDElement = page.Element("CategoryID");

    if(categoryIDElement != null && categoryIDElement.Value !="")

               {

                    var taxonomyManager = session.TaxonomyManager;

                    var matchingCategory = taxonomyManager.Category(categoryIDElement.Value);

                    if(matchingCategory != null)

                    {

                        matchingCategory.Name = newName;

                    }

                }

}

Prevention of Special Characters in Page Names

The next example works with something a little simpler: preventing users in my CMS from renaming a page to anything containing special characters.

You might choose to do this in your CMS because, even though the CMS automatically cleans up names of pages when it creates URLs for those pages, the team who manages content in the CMS gets confused by messy or unusual page names.

Component or folder names don’t need to be checked or cleaned up, since they don’t make URLs, so start by making sure that the item being renamed is a page. Assets are immediately filtered out since there is a different hook for those.

if(!page.IsComponent)

{

    var pattern = @"[!@#\$%\^&\*\(\),\.\?"":{}\|<>\[\]\\\/]";

    var regex = new Regex(pattern);

    var matches = regex.Matches(newName);

    if (matches.Count > 0)

    {

        throw new System.ArgumentException("Prohibited Characters in Page Name. Please avoid these characters when naming a page: \'\"/|\\`~!@#$%^&*()");

    }

}

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: June 6, 2022
  • LAST UPDATED: September 18, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.