Sitecore create custom Content Editor Warnings using Conditions and Rules

The Content Editor Warnings are these yellow boxes that hover over your content in the Content Editor in Sitecore:

Content Editor Warning
Content Editor Warning

In the old days we would make them by hooking into the getContentEditorWarnings pipeline. Nowadays we use the Sitecore Rules engine. So to make your own Content Editor Warning you need to:

  • Create a custom Condition.
  • Create the code for the custom Condition
  • Create a new Rule

STEP 1: CREATE A CUSTOM CONDITION

Custom Condition
Custom Condition

Conditions are placed under:

/sitecore/system/Settings/Rules/Definitions/Elements

You can create your own folder in this structure and create a “Condition“. The “Text” field should contain the text displayed in the Rule editor, and the “Type” field should contain the reference to the class containing the code, for example:

MyCode.Rules.MyRule, MyDll

STEP 2: CREATE THE CODE

A condition inherits from a RuleContext. Rules can return different things, my return true or false, but you can create rules that return values like integers or strings.

This is my Rule:

using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;

namespace MyCode.Rules
{
  public class MyRule<T> : WhenCondition<T> where T : RuleContext   
  {
    protected override bool Execute(T ruleContext)
    {
      Assert.ArgumentNotNull(ruleContext, "ruleContext");
      // This is them item being clicked on
	  Item item = ruleContext.Item;
	  // Pseudocode, imagine that you are checking
	  // something
	  if (something == true)
        return true;
      return false;
    }
  }
}

STEP 3: CREATE A NEW RULE

Content Editor Warning Rule
Content Editor Warning Rule

Content Editor Warning rules are placed in:

/sitecore/system/Settings/Rules/Content Editor Warnings/Rules

And you need to create a new Content Editor Warning Rule.

Then you create the rule using the rules editor:

Rules Editor
Rules Editor

My rule is fancy, as a first check for a specific template type (built in rule), then check for my own rule.

Lastly, the rule ends with what should happen. Here you type in the text that should appear in the content editor warning.

That’s it. You are now a Sitecore expert.

MORE TO READ:

Advertisement

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, General .NET, Sitecore 5, Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.