Sitecore Custom Rule (Action and Condition)

I have a situation to create a custom Sitecore rule and action for it.

v  Goto /Sitecore/system/Settings/Rules/Definitions/Elements
v  Create a new item base on the template “ElementFolder” with a name “Custom Rule
v  Click on the item and create condition and Actions that are necessary for your requirements
Condition Template Item:
It has two important sections
v  Data
·         Text (This is the condition Text which will use tokens to evaluate the condition and user input which will be in [ ] brackets.The brackets contain four positional parameters separated by commas (“,”).
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/Common/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. Display Text to the user.
Example:
where the current template name [OperatorId, Template,,compares to] [value,,,value]
v  Script
·              Type — The.NET class signature (Namespace.Class, assembly).
·              Code — Inline code in any of the languages supported by the .NET framework.
·            References — List of .NET assemblies to reference, separated by commas (“,”).
·              Language — A .NET language specifier, such as CSharp for C#.

To create a custom condition, inherit from either
v  Sitecore.Rules.Conditions.OperatorCondition,
v  Sitecore.Rules.Conditions.WhenCondition
v  Sitecore.Rules.Conditions.StringOperatorCondition

public class CheckspecificTemplate<T> : StringOperatorCondition<T> where T : RuleContext
    {
        public string Value { get; set; }

        protected override bool Execute(T ruleContext)
        {           
            var itemTemplateName = ruleContext.Item.TemplateName;
            return Compare(itemTemplateName, Value);
        }
    }


Action Template Item:

·         It also has two field section similar to Condition Template
·         To create a custom condition, inherit from RuleAction

public class SetNewTitle<T> : RuleAction<T> where T : RuleContext
    {


        public override void Apply(T ruleContext)
        {
            using (new SecurityDisabler())
            {
                ruleContext.Item.Editing.BeginEdit();
                ruleContext.Item["Title"] = "NewTitle";
                ruleContext.Item.Editing.EndEdit();
            }
        }


    }

v  Goto /sitecore/system/Settings/Rules/Definitions/Tags and create “Custom Rule” Item based on the template “/sitecore/templates/System/Rules/Taxonomy/Tag

v  Goto /sitecore/system/Settings/Rules and create “Custom Rule” item based on the template
/sitecore/templates/System/Rules/Taxonomy/Rules Context Folder
     
Condition:

Action:

v  Goto newly created “Custom Rule” and select item /Sitecore/system/Settings/Rules/Definitions/Elements/Custom Rule/Tags/Default and assign the Field “Tags” with Custom Rule from Multilist

Now we have created condition and Action, so its time to trigger it. But before that, we need to create a rule to execute the condition and action that we defined.
Create a Rule in the following path /sitecore/system/Settings/Rules/Insert Options/Rules/ with item name “CustomRuleFromInsertOptions” and update Name and Rule field.

Result:
Now based on the condition and Action defined the Title of the item gets updated.



Reference :Rules_engine_cookbook_sc70-a4.pdf

Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview