3

Normally we do things like Sitecore.Context.PageMode.IsExperienceEditorEditing to detect if the experience editor is running. I want to do something similar, but detect if the live site is running instead.

I found Sitecore.Context.PageMode.IsNormal, but this also returns true from within Content Editor.

What's the best way to achieve this?

  • What about checking for the context database? The live site should use the web database – Dražen Janjiček Nov 23 '16 at 20:00
  • When you say "live site" you mean from the CD server and/or a page is being viewed? Where are you testing the PageMode, in a component or in a pipeline? – jammykam Nov 23 '16 at 20:00
  • @jammykam, a data provider. – Dylan McCurry Nov 23 '16 at 21:23
3

You could do something like the following:

Sitecore.Context.PageMode.IsNormal && Sitecore.Context.Site.Name == "website"

Obviously, you would want to replace "website" with the name of the site that you are looking for, if different. You could, of course, enhance this solution to support comparing the sites in another way, as well or you could invert the solution by saying:

Sitecore.Context.PageMode.IsNormal && Sitecore.Context.Site.Name != "shell"

This will resolve the issue you described, by returning false if in the Content Editor, but the better solution would be to match directly against the site you are looking for, if possible.

  • I would agree this suggestion but care needs to be taken on resolving the correct site using Sitecore.Context. If you use the preview for example in the content editor you won't get the "shell" site. There's a solution for this here - firebreaksice.com/sitecore-context-site-resolution – Ian Graham Nov 23 '16 at 20:29
  • 1
    Very true, @IanGraham. I actually use Mark Ursino's solution in all of my new builds. I do believe that I remember reading that this was improved, however, in Sitecore 8.1, in a release note. I will have to go back and test, but in either case Mark's solution is solid. I will update my post next chance I get (I am mobile, now). – Zachary Kniebel Nov 23 '16 at 20:35
  • Great, would be interested to know if that is no longer an issue in 8.1+. – Ian Graham Nov 23 '16 at 20:44

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.