8

Can I know how to detect user is in page editor mode using code? This is because, I have a component, when user browses from page editor, it will search in master_index folder instead of web_index folder.

Dan Solovay
  • 3,134
  • 3
  • 26
  • 55
WenHao
  • 1,183
  • 1
  • 15
  • 46

3 Answers3

27

Please check with : if (Sitecore.Context.PageMode.IsPageEditorEditing)

also is working on Sitecore 6.6, it not depends on Sitecore 7.

Please check also this Sitecore blog post by Martina Welander.

To check if is normal page use: if (Sitecore.Context.PageMode.IsNormal)

To check if is preview mode use: if (Sitecore.Context.PageMode.IsPreview)

Also are others PageMode:

  • IsPageEditorClassic
  • IsPageEditorDesigning
  • IsPageEditorEditing
  • IsPageEditorNavigating, etc

If you are curious you can check with Reflector or dotPeek this class: Sitecore.Context.PageMode from Sitecore.Kernel assembly.

Shriroop
  • 1,174
  • 2
  • 16
  • 28
  • 1
    Sitecore 8 switched to "Sitecore.Context.PageMode.IsExperienceEditor". And if you're using Glass, you can shorthand to "@IsInEditingMode" in the view. – Ben Sewards Aug 22 '17 at 19:35
2

Rather than checking the page mode, I think you just want to piggyback on the context database name -

var indexName = Sitecore.Context.Database.Name + "_index";
Pavel Jounda
  • 219
  • 5
  • 14
Paul George
  • 1,788
  • 16
  • 33
  • I tend to agree. Although it doesn't answer the original question, it's a better solution to the underlying problem. – Dan Sinclair Oct 27 '16 at 20:36
0

I am using this syntax in view to add a class for JS detection, same code can be used for back-end detection with bit modification:

<html class="@(Sitecore.Context.PageMode.IsPageEditor ? "inexpeditor" : "notexpeditor")">
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Sunny
  • 863
  • 9
  • 10