Use case

Recently I had to build a functionality to automatically fill a field in Sitecore when saving a specific item. The use case was as follows: Every time the editor saves an Article Content Page, with an empty Page Description field, the content of the Introduction field should be copied and saved to the Page Description field.

Solution: Sitecore Rules!

After a bit of research, I (re)discovered Sitecore Rules and it was quite easy to implement a solution for my problem.

How to solve the given use case

Sitecore backend item configurations

First, you create a new Element Folder in Sitecore/System/Settings/Rules/Definitions/Elements and name it according to  your project (in this article I will use the company name TAC as an example). Within this folder, you create a new Action called “Copy Field to Field”.

rules_tree

Adding a new class to Sitecore using Visual Studio

In your Visual Studio Solution, you create a new class with the same name:

Update Sitecore Rule Action

Afterwards, you can complete the fields in your Action item:

rule_action

In the Data section’s text field you write the text the user will see in the Rule Editor. With the help of square brackets, you can define parameters that are then passed into your .NET class. Those parameters will be displayed as editable links in the Rule Editor. Within the brackets, the parameters can be defined in more detail. There are four positional parameters separated by commas. In the Rules Engine Cookbook there is a good description of those:

  1. The name of the property that the parameter sets in the .NET class that implements the condition.
  2. The name of an item beneath the /Sitecore/System/Settings/Rules/Definitions/Macros item providing predefined functionality, or an empty string. For example, to activate a user interface allowing the user to select an item from the content tree, specify the value tree.
  3. URL parameters to the user interface activated by the macro specified by the previous parameter, or an empty string. For example, to specify the /Sitecore/Content/Home item as the root for a selection tree, enter root=/sitecore/content/home.
  4. Text to display if the user has not specified a value for the parameter.

In our case, only the first and fourth parameter is necessary, because the user should enter the field name as a string. (I tried using the tree to get the field ID, but as I don’t know on which fields the rule will be used, the tree dialog has to start at the root item. And then there is a lot of clicking on the user’s side to navigate to the specific field.)
In the Type field of the Script section, you just enter the type signature of your .NET class. First the namespace, afterwards the containing project, comma separated.

Configuring Sitecore Rule conditions

To create a rule with your condition, navigate to /Sitecore/System/Settings/Rules/Item Saved/Rules and create a new Rule. Call it “Autofill SEO Fields on Article Page”, as this was the initial task.

To open the Rule Editor you click on “Edit rule”. You then choose your conditions, which need to be true in order for your Action to run, and finally you add the action.

[caption id="attachment_5394" align="alignnone" width="300"]Rule Editor. Click for bigger picture. Rule Editor. Click for bigger picture.[/caption]

rule_editor_rule rule_data

The result - use cases resolved using the Sitecore Rules engine!

Now, every time you save an Article Content Page with an empty Page Description field, the content of the Introduction field is copied to the Page Description field. Mission accomplished ;-)