• About this blog
  • About me
  • Contact
Sitecore XperiencesThe things I've seen as a Sitecore Professional
  • About this blog
  • About me
  • Contact
Menu
  • About this blog
  • About me
  • Contact
  • 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.

    Creating the action

    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.

    Rule to rename

    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”

    • kami says:
      July 3, 2015 at 10:10 am

      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.

      Reply
      • Rodrigo Peplau says:
        July 31, 2015 at 10:40 pm

        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.

        Reply

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Proudly 10x Sitecore MVP!
(2016-2025)

Localization

Recent Posts

  • All Submissions from Sitecore Hackathon 2025!
  • Know your Media Library disk usage with a simple PowerShell script
  • Automated Behavioral Personalization in Sitecore XP with Generative AI
  • MVPinny knows the Sitecore Accelerate for Partners
  • Rodrigo Peplau wins Sitecore Most Valuable Professional award

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

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • All Submissions from Sitecore Hackathon 2025!
  • Know your Media Library disk usage with a simple PowerShell script
  • Automated Behavioral Personalization in Sitecore XP with Generative AI
  • MVPinny knows the Sitecore Accelerate for Partners
  • Rodrigo Peplau wins Sitecore Most Valuable Professional award
  • Error enabling GraphQL IDE with XM Cloud
  • Meet MVPinny: the AI-Powered Sitecore Assistant
  • Content generation with Sitecore Connect and ChatGPT
  • XM Cloud Starter Kit with Content Hub Integration
  • Meet the (Brand new) Advanced Powershell Packaging Features

Tag Cloud

    Architecture Automation Continuous Integration Debugging Development Environments Hackathon How To PhantomJS Powershell Server Roles TDS Team City Team Development for Sitecore XConnect

Pages

  • About this blog
  • About me
  • Contact

Search

Copyright © 2015 Rodrigo Peplau Theme created by PWT. Powered by WordPress.org