Go Back

Prevent code from running in Edit or Preview mode

Sitefinity CMS has a built-in WYSIWYG page editor.  WYSIWYG is an acronym for What You See Is What You Get.  This term is used to describe a system in which content displayed during editing looks similar (if not identical) to the final output

The alternative to WYSIWYG page editing is a sandboxed composition mode where content looks nothing like the final results.  In this environment, editors must constantly toggle into a preview mode to see the results of content changes.  Sitefinity deliberately choose a WYSIWYG editing experience to minimize the steps (and pain) needed to edit, preview & publish content changes. 

However, to create an accurate representation of the final page Sitefinity needs to execute the controls & code used on the page.  In some circumstances, this can create problems.  Thankfully there is an easy solution to this problem.

Disabling WYSIWYG

A few weekends ago during the WAM2010 charity event, I used an XML Sitemap User Control created by Sitefinity Partner Roger West Creative.  This control creates an XML representation of a web site’s sitemap that can be easily consumed by search engines.

To do this, the control needs to override Sitefinity’s default page rendering and instead create an XML document.  This functionality would wreak havoc in Sitefinity’s page editor.  In fact, once the control is dropped onto the page it would be impossible to Edit or Remove.

To avoid these problems the control needs disabled when in edit mode.  Sitefinity adds a querystring parameter (cmspagemode) to the URL each time a page is being edited (or previewed):

Sitefinity CMS URL in Page Edit Mode

  • cmspagemode=edit – Sitefinity is in page edit mode.
  • cmspagemode=preview – Sitefinity is in page preview mode.

To prevent Sitefinity from executing the XML Sitemap Control Roger West added a small bit of code to disable the control’s functionality when it is running in preview or edit mode.

protected void Page_Load(object sender, EventArgs e){    // Create output only if page is not in cms edit/preview mode    if (Page.Request["cmspagemode"] == null)    {        // This code will not be executed when Sitefinity is in edit/preview mode    }}

That’s it.  Any code wrapped inside will not be executed when Sitefinity is in edit or preview mode.  If necessary, WYSIWYG editing can now be disabled.

Comments  3

  • Josh 03 Feb

    For a strongly-typed way of checking, try this:

    var p = Page as Telerik.Cms.Web.CmsPageBase;  
    if (p.PageMode == CmsPageMode.Live)  
    {  
         // do that voodoo  
  • Mark 05 Feb

    Just an FYI, you might be careful about likely copyrighted images on your blog. I am 99.9% sure that image is from the Apple Dev area.
  • Gabe Sumner 05 Feb

    Hey Mark.  Thanks for the heads up.  It was just a random icon I found on some icons web site.  I removed it from the page.
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: