4

Is there any way to check Page mode like edit/preview/normal using Sitecore Rule? I want to render specific field in rendering variant of SXA component based on page mode (editor/preview).

7

You need to create your custom rule for this.

Create a class which inherits StringOperatorCondition:

using Sitecore;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;

namespace ClientName.Foundation.SitecoreExtensions.Rules
{
    public class IsExperienceEditorMode<T> : StringOperatorCondition<T>
        where T : RuleContext
    {
        protected override bool Execute(T ruleContext)
        {
            return Context.PageMode.IsExperienceEditor;
        }
    }
}

You can even check whether somebody is editing by using Context.PageMode.IsExperienceEditorEditing instead.

You can also use Context.PageMode.IsPreview to check whether you are in preview instead of checking whether you are in Experience Editor.

Decompile with ilSpy or dotPeek Sitecore.Kernel.dll and go to Sitecore namespace and under Context, there is PageMode and various other modes that you can check and use:

enter image description here

You also need to create this rule in Sitecore. Do it under /sitecore/system/Settings/Rules/Definitions/Elements/ClientName/ or anywhere under /sitecore/system/Settings/Rules/Definitions/Elements/. Create new item based on /sitecore/templates/System/Rules/Condition template. Text would be "where in Experience Editor mode" or anything else you like. Type will be classname with namespace with assembly name specified like ClientName.Foundation.SitecoreExtensions.Rules.IsExperienceEditorMode, ClientName.Foundation.SitecoreExtensions.

Here you have it as picture:

enter image description here

2

There is no ootb exact check for page mode - you could write it yourself if you want. Or you could try to work with the querystring parameters. Not sure if that is always correct, but a quick test seemed to work with a rule like this:

where the sc_mode request parameter is case-insensitively equal to edit

The sc_mode parameter normally has the page mode - the example above tests on the experience editor.

  • Nice one :-). But I should point out, this won't actually tell if the user is in Editing or Designing modes; just that the Experience Editor is active in some form. – Mark Cassidy Nov 12 at 12:41
  • You should be able to detect preview, xp editing and explore/debug modes. A custom rule would be better indeed, just pointing out a quick solution here.. – Gatogordo Nov 12 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.