Skip To Content

Kentico Tip: How to Remove Preview Mode

It's always important to preview content changes before they are published, and Kentico easily provides the ability to do so. While this is a great feature, it also creates an unwanted side effect when you’re working in the Pages application.

Today, I'll explain the best ways to work around this issue.

The Quick Fix

When previewing content pages prior to publishing, if you navigate to the page tab on any page, and then (in the same browser session) visit a live page, you’ll see the following bar at the top:

Kentico helper text to close preview mode

When you see this, some functionality in templates and web parts may not work properly, so you must close the preview mode before you continue. Easy enough. But because this setting is cookie-based, it will persist each time you reload the Page tab in that browser. This can become annoying when you have a large number of changes in a row that you'd like to preview.

A simple trick to avoid continuously closing preview mode is to implement private browsing mode for your testing window, or better yet, try two different browsers for development and testing.

A Permanent Solution

If you are not using preview mode on your site, another option you have is to simply disable it. If you want to disable preview mode completely, you can do so by adding the following code to PortalTemplate.aspx:

protected override void OnPreInit(EventArgs e)

{

 base.OnPreInit(e);

 // If Preview mode (Cookie CMSViewMode == 3), set to Live Site (Cookie CMSViewMode == 0) 

 if (PortalContext.ViewMode == CMS.PortalEngine.ViewModeEnum.Preview)

 {

 PortalContext.ViewMode = CMS.PortalEngine.ViewModeEnum.LiveSite;

 }

}

You can also put this code directly in a web part, but note that due to the page event lifecycle, it may not work on the first page load because the web part’s PreInit event is called after PortalTemplate’s. Additionally, you may want to add some conditional logic as well, such as checking if the current user is a Global Administrator.

We hope this is a helpful tip! In general, we try to avoid modifying any core Kentico code, but sometimes we do have to make changes to PortalTemplate.aspx in order to add body tags or other custom markup. While it’s not always ideal to do so, it’s nice to know that the option exists if needed.

Have any Kentico-related quandaries you need assistance with? Just ask! Our BlueModus team has been working with the platform for over a decade and would be happy to help.