我們在開發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了。