C# - find if in console preview

Topics: General, Troubleshooting
Mar 8, 2012 at 7:32 PM

I feel like I should be able to find this, but I've not been able to and Request isn't working for me for some reason.

I have a C# function which needs to know if the current request is in the C1 Console Preview mode or if it's live.


Mar 8, 2012 at 7:46 PM

preview mode, like the preview-tag on a page?

Mar 8, 2012 at 7:51 PM

Preview mode, like when you're in the Content section of the Console and you click on the Preview tab of a page to see unpublished changes.

Mar 8, 2012 at 7:56 PM
Edited Mar 8, 2012 at 7:58 PM

technically, using the Preview-tab is not the same as viewing a unpublished page. You basically have three modes

  1. Preview-tab: here its possible to view unsaved changes to page content. You can't click on links or browse to other pages in this mode.
  2. Unpublished: possible to view a saved page before its made public. You can click links and browse as much as you like
  3. Published: The saved page made publicly available.

for checking if the page is being rendering in the preview-tab, you would chech the previewKey querystring

var isPreview = HttpContext.Current.Request.QueryString["previewKey"] != null;

another way is to use the RenderingContext-class, but its api just feels wrong to me

var renderingContext = RenderingContext.InitializeFromHttpContext();
var isPreview = renderingContext.PreviewMode;
Mar 8, 2012 at 10:21 PM

Thanks, that first one worked.

Mar 9, 2012 at 7:56 AM

I recommend the following code, note that it would also return true when you previewing by clicking "View unpublished" on a page.

bool isUnpublished = Composite.Data.DataScopeManager.CurrentDataScope == Composite.Data.DataScopeIdentifier.Administrated;

Mar 9, 2012 at 8:51 AM


that is for checking if the mode is unpublished... it doesn't tell you if the page is rendered in the "Preview"-tab

Mar 9, 2012 at 12:04 PM

@burningice would it work given the application - yes :)