Define sharepoint page display mode

I have this question many times and bored, trying to find a good solution. I don’t understand why microsoft does not include a method that can easily determine the page display mode: “normal display” or “development mode”. He has a lot of tips for checking different variables, but he cannot say for sure that a page in design is on different types of pages (web page page and wiki page) and on the reverse side or not.

I'm finally tired, and I'm writing this:

    public static bool IsDesignTime()
    {
        if (SPContext.Current.IsDesignTime) return true;

        if (HttpContext.Current.Request.QueryString["DisplayMode"] != null)
            return true;

        var page = HttpContext.Current.Handler as Page;

        if(page == null) return false;

        var inDesign = page.Request.Form["MSOLayout_InDesignMode"];
        var dispMode = page.Request.Form["MSOSPWebPartManager_DisplayModeName"];
        var wikiMode = page.Request.Form["_wikiPageMode"];
        var we = page.Request.Form["ctl00$PlaceHolderMain$btnWikiEdit"];

        if (inDesign == null & dispMode == null) return false; //normal display

        if (we == "edit") return true; //design on wiki pages

        if (page is WikiEditPage & page.IsPostBack & inDesign == "" & dispMode == "Browse" & wikiMode == "") return false; //display wiki on postback


        if (inDesign == "" & dispMode == "Browse" & (wikiMode == null | wikiMode == "")) return false; //postback in webpart pages in display mode

        if (inDesign == "0" & dispMode == "Browse") return false; //exiting design on webpart pages

        return true;
    }

Does anyone have a better solution?

+5
source share
4 answers

you have 2 cases for detecting page mode:

If you use the team site:

    if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
    {
        ltr.Text = "EditMode2";
    }
    else
    {
        ltr.Text = "ViewMode";
    }

if you use the publication site:

if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   // your code to support display mode
  }
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   // your code to support edit mode
  }
+12

WebpartPage, ,

 WebPartManager mgr = this.WebPartManager;
 if (mgr.DisplayMode == WebPartManager.EditDisplayMode)
    {
        // logic when in Edit Mode
    }
 else
    {

    }
+5

It was hard for me to get any of these answers to work in Sharepoint 2013, given all the scenarios. I also could not get the EditModePanel to work sequentially. I found a snippet in this article that seems to work in all the scripts I've tried so far.

var isPublishing = SPContext.Current.FormContext.FormMode != SPControlMode.Invalid;
var wpDMode = WebPartManager.GetCurrentWebPartManager(Page).DisplayMode.Name;
var isEditing = isPublishing
     ? SPContext.Current.FormContext.FormMode != SPControlMode.Display
     : (wpDMode.Equals("Edit") || wpDMode.Equals("Design"));

Then you can just check isEditingfor your conditions.

+3
source

try this code ..

if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   // your code to support display mode
  }
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   // your code to support edit mode
  }
+2
source