【问题标题】:Determine display mode of sharepoint page确定sharepoint页面的显示方式
【发布时间】:2019-02-09 07:13:55
【问题描述】:

我多次遇到这个问题,并且在尝试找到好的解决方案时感到很无聊。 不明白为什么微软不包含可以轻松确定显示页面模式的方法:“正常显示”或“设计模式”。 它有许多检查不同变量的建议,但不能在不同类型的页面(webpart 页面和 wiki 页面)上以及是否在回发上唯一说明该页面的设计。

我终于累了,我写这个:

    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;
    }

有人有更好的解决方案吗?

【问题讨论】:

  • 在发布页面访问 page.Request 属性失败并出现异常。 (可以使用 HttpContext.Current.Request 代替。
  • 对于 wiki 页面,并且只有 wiki 页面,您的代码给了我解决方案:page.Request.Form["ctl00$PlaceHolderMain$btnWikiEdit"] == "edit"。 FormMode 负责处理非 wiki 页面。

标签: c# asp.net sharepoint-2010


【解决方案1】:

您有 2 种情况来检测页面模式:

如果您使用的是团队网站:

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

如果您使用的是发布网站:

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】:

    如果您在 WebpartPage 中的工作比下面的代码更适合我

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

    【讨论】:

    • 我在阅读本文时考虑到了 WebPartPage。这是一个好的开始,这篇 SPTechBytes 文章展示了 3 种主要模式之间的区别。
    • page.Request.Form["ctl00$PlaceHolderMain$btnWikiEdit"] == "edit" 适用于 wiki 页面。以上代码适用于非 wiki 页面。
    【解决方案3】:

    考虑到所有场景,我很难在 Sharepoint 2013 中找到这些答案。我也无法让EditModePanel 始终如一地工作。我在this article 中找到了一个 sn-p,它似乎适用于我迄今为止尝试过的所有场景。

    注意:这在 Page_Init 中不起作用,但在 Page_Load 中起作用

    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"));
    

    然后您可以简单地检查isEditing 以了解您的条件。

    【讨论】:

      【解决方案4】:

      请试试这个代码..

      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
        }
      

      【讨论】:

      【解决方案5】:

      处理 SharePoint 页面模式。

      这对我有用,我通过以下几行解决了我的关键问题。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-01
        • 2021-08-21
        • 1970-01-01
        相关资源
        最近更新 更多