Using Rules to replace spaces by dashes in Sitecore 7.5 in 3 simple steps

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.

 

Posted in Actions, Development, Rules
2 comments on “Using Rules to replace spaces by dashes in Sitecore 7.5 in 3 simple steps
  1. kami says:

    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.

Leave a Reply

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

*

  Am Not Spammer

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>