-
Using Rules to replace spaces by dashes in Sitecore 7.5 in 3 simple steps
April 28, 2015 Rodrigo Peplau 2
In this article I show how to use rules to rename items so, every time an item is saved, spaces gets replaced by dashes. Advantage of using rules is that you can visually setup different kind of conditions, without having to hardcode them at your code.
1) Create the Action that triggers the item renaming
In your master database, add your new Action using the template /sitecore/templates/System/Rules/Action. In this case I’m using the path /sitecore/system/Settings/Rules/Definitions/Elements/Script because the Script Element Folder is already referenced by the Item Saved rules.
The Type field must point to your class that handles the item renaming (use the format “Namespace.ClassName, Assembly“)
2) Create the class that executes the item renaming
This is where your code for checking and renaming the item gets executed (parameter “ruleContext.Item” brings the item being saved).
using Sitecore.Diagnostics; using Sitecore.Rules; using Sitecore.Rules.Actions; namespace BusinessLayer.Rules.Actions { public class AddHyphensToItemName : RuleAction where T : RuleContext { public override void Apply(T ruleContext) { Assert.ArgumentNotNull(ruleContext, "ruleContext"); var obj1 = ruleContext.Item; if (obj1 == null) return; // Scape if no space is at the Name var itemName = obj1.Name; if (!itemName.Contains(" ")) return; // Rename the item itemName = itemName.Replace(" ", "-"); obj1.Editing.BeginEdit(); obj1.Name = itemName; obj1.Editing.EndEdit(); } } }
3) Create the Rule to execute the Action above
Add your new Rule using the template /sitecore/templates/System/Rules/Rule, then edit your rules.
In this case what I’m doing is:
- Including everything under “/sitecore/content/Site/Home” – because I just want to apply this rule to the items under my Homepage;
- Skipping items that doesn’t have a layout – We don’t mind if items without renderings has spaces;
- Calling the action “Add Hyphens To Item Name” for the matching Items.
Categories: Actions, Development, Rules
Visual Studio Solution at Sitecore Projects - Avoiding IIS restarts with Robocopy in a Post Build event Advanced display of SVG Images in Sitecore
2 thoughts on “Using Rules to replace spaces by dashes in Sitecore 7.5 in 3 simple steps”
Leave a Reply Cancel reply
Proudly 10x Sitecore MVP!
(2016-2025)
Localization
Recent Posts
Recent Comments
- navan on Meet MVPinny: the AI-Powered Sitecore Assistant
- Adriana on Content generation with Sitecore Connect and ChatGPT
- NAVAN on Automatic Sitecore NuGet upgrades with Powershell
- Hedipo S Menezes on Corey Peplau wrote this - WFFM conflict with Unity DI and a lesson on how Sitecore community is so amazing
- Rodrigo Peplau on ERROR [Content Testing]: Cannot find PhantomJS executable at ' (...) /data/tools/phantomjs/phantomjs.exe'. Aborting screenshot generation.
Archives
- March 2025
- January 2025
- June 2024
- April 2024
- February 2024
- December 2023
- November 2023
- August 2023
- July 2023
- January 2023
- February 2022
- December 2021
- November 2021
- March 2021
- July 2020
- February 2020
- September 2019
- July 2019
- April 2019
- March 2019
- December 2018
- February 2018
- January 2018
- November 2017
- September 2017
- August 2017
- July 2017
- March 2017
- February 2017
- November 2016
- September 2016
- August 2016
- July 2016
- April 2016
- November 2015
- September 2015
- July 2015
- April 2015
- March 2015
- February 2015
Categories
- Actions
- Active Directory
- Analytics
- Architecture
- Bug fixing
- CDP/Personalize
- ChatGPT
- Content Edition Experience
- Content Hub
- Continuous Integration
- Dev
- Development
- Environments
- Experience Editor
- Experience Forms
- Front-end
- Hackathon
- Health Check builds
- Helix
- How To
- LDAP
- MVP
- MVP Summit
- MVPinny
- Phantom JS
- Powershell
- QA
- Richtext Editor
- Rules
- Security Provider
- SIF
- Sitecore 9
- Sitecore API
- Sitecore Community
- SItecore Connect
- Sitecore Modules
- Sitecore Rocks
- Sitecore Rule Processor
- Sitecore Symposium
- SPE
- SPE-only Alliance
- SPEAK
- SUG
- Support Ticket
- TDS
- Team City
- Uncategorized
- Upgrades
- Visual Studio
- WFFM
- Workflow
- XConnect
- xDB
- XM Cloud
In the third step “3) Create the Rule to execute the Action above” .
How did you actually added the action to the rule? I am referring to the 3rd picture. How did you added the action “Add Hyphens to Item Name” in the Rule 1.
Hello Kami,
The action “Add Hyphens to Item Name” is added to Sitecore when you run Step 1 in the article:
“In your master database, add your new Action using the template /sitecore/templates/System/Rules/Action. In this case I’m using the path /sitecore/system/Settings/Rules/Definitions/Elements/Script because the Script Element Folder is already referenced by the Item Saved rules.”
If you simply duplicate the “Run Script” item at the first screenshot and fill the Type correctly with your assembly (Step 2), you will have the “Add Hyphens to Item Name” action available when you’re editing the Rule. All you have to do then, is to select that action at your condition (Step 3), and your code will trigger when the condition is reached.