Temporarily Disabling Experience Editor Rendering

One of Sitecore’s defining features is its Experience Editor (previously known as Page Editor). In order make an item’s field editable in the Experience Editor we need to pass it through the Render method found in Sitecore.Web.UI.WebControls.FieldRenderer. The Render method will detect whether or not the current page is in Experience Editor mode, and depending on that it will either wrap the value in javascript, or render the value of the field. It also conveniently creates and tags if the field is an Image or Link type, respectively. Sometimes there is code that uses the Render method, but there is a context in which we don’t actually want the Experience Editor javascript to wrap the value of field. The code could be in,

  • a helper method that process some content type
  • a model that has fields pre-rendered
  • a view which uses the standard Sitecore helpers for rendering fields
  • any number of other places where Render may be used

The reason for wanting to temporarily disable the Experience Editor wrapper around the result of the Render method could be,

  • Reusing a view in a place where we want to restrict editing
  • Reusing a model in a complex piece of HTML that is manipulated by JS
  • Reusing a model or a helperĀ method in some back end codeĀ that would break if Experience Editor tags were present around the data that it expects.

Using the following IDisposable, you can temporarily disable the Experience Editor portion of Sitecore’s FieldRenderer Render method. This code will work in back end code as well as CSHTML templates.

public class FieldRendererEditingDisabler : IDisposable
   public FieldRendererEditingDisabler()
      this.OriginalDisplayMode = Context.Site.DisplayMode;
      if (this.OriginalDisplayMode == DisplayMode.Edit)
         Context.Site.SetDisplayMode(DisplayMode.Normal, DisplayModeDuration.Temporary);

   private DisplayMode OriginalDisplayMode { get; }

   public virtual void Dispose()
      if (this.OriginalDisplayMode != Context.Site.DisplayMode)
         // Resetting back to original display mode, keeping temporary because there is no need to re-write the cookie
         Context.Site.SetDisplayMode(this.OriginalDisplayMode, DisplayModeDuration.Temporary);

The usage of the the FieldRendererEditingDisabler is:

//once you want to disable ExperienceEditor
using (var disabler = new FieldRendererEditingDisabler())
  //your code here
//Experience Editor is re-enabled

Leave a Reply

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