代码改变世界

MOSS 2013研究系列---隐藏Ribbon

2014-07-10 08:54  温森特  阅读(1827)  评论(4编辑  收藏  举报

  我们在开发Sharepoint 2013 的时候,常常需要隐藏Ribbon,那个Ribbon是属于Office的特征,但是我们做门户的时候,大家都不希望看见到它,但是我们又离不开它,以管理的身份进行设计列表或文档库的时候,必须需要它,没有它的话,很多功能就无法实现了。基于以上背景需求,我们可以利用母版页里面的SPSecurityTrimmedControl控件,它的用途就是识别当前用户在网站的角色,有了它就比较好办了,我们找到Ribbon 的样式class,在自己的样式表中,我们隐藏掉它,当是管理员角色登录的时候,我们就显示出Ribbon,如下代码:

 

        <!--MS:<SharePoint:SPSecurityTrimmedControl runat="server" AuthenticationRestrictions="AuthenticatedUsersOnly" Permissions="AddAndCustomizePages">-->
          <script type="text/javascript">
           document.getElementById("s4-ribbonrow").style.display = "block";
           document.getElementById("suiteBar").style.display = "block";

          </script>
        <!--ME:</SharePoint:SPSecurityTrimmedControl>-->

 

如此简单就解决了,其实还有好几个办法也能实现。

  Javascript的实现方案:

function ShowRibbon() {

   $("#s4-ribbonrow").show();

   $("#s4-workspace").height($(document).height() - $("#s4-ribbonrow").height() * 2);

}

function HideRibbon() {

   $("#s4-ribbonrow").hide();

   var newHeight = $(document).height();

   if ($.browser.msie) {newHeight = newHeight - 3; }

      $("#s4-workspace").height(newHeight);

}

_spBodyOnLoadFunctionNames.push("HideRibbon");


封装好JS文件,然后用VS开发工具,打包成解决方案包,直接部署就OK了。