總結隱藏Ribbon選單的方法

一隻老鼠發表於2013-07-02
1. 過載
using (SPSite site = new SPSite("http://SP2010-01"))
{
 using (SPWeb web = site.OpenWeb())
 {
  SPList list = web.Lists.TryGetList("Shared Documents");
  web.AllowUnsafeUpdates = true;
  SPUserCustomAction actionView = list.UserCustomActions.Add();

   StringBuilder newurl = new StringBuilder();
  newurl.AppendLine("<CommandUIExtension>");
  newurl.AppendLine("     <CommandUIDefinitions>");
  //Provide Location ID to hide the button
  newurl.AppendLine("        <CommandUIDefinition Location=\"Ribbon.Documents.New.NewFolder\">");
  newurl.AppendLine("        </CommandUIDefinition>");
  newurl.AppendLine("    </CommandUIDefinitions>");
  newurl.AppendLine("</CommandUIExtension>");

   actionView.Location = "CommandUI.Ribbon";
  actionView.Title = "RemoveNewFolderButton";
  actionView.CommandUIExtension = newurl.ToString();
  actionView.Update();

   web.AllowUnsafeUpdates = true;
 }
}

 

2. 後臺程式碼控制
SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
if (ribbon != null)
{
ribbon.TrimById(“RibbonGroupId”);
}

3. 使用javascript控制

需要引用到

  • Core.js
  • CUI.js
  • Init.js
  • SP.Ribbon.js
//使用RIBBON API
<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
 
   <!-- ScriptLink tags for sp.js and CUI.js could go here if you wish -->
 
   <script language="javascript" type="text/javascript">
 
      function DoSomethingWithRibbon() {
         // Gets a reference to a CUI.Ribbon object (CUI.js)
         var ribbon = SP.Ribbon.PageManager.get_instance().get_ribbon();
 
         // Show me which tab is selected - will show
         // 'Ribbon.Read' if the Browse tab is selected.
         alert(ribbon.get_selectedTabId());
      }
 
      // Note: 'SOD' is an abbreviation for "Script on Demand"
      SP.SOD.executeOrDelayUntilScriptLoaded(function() {
 
         var pm = SP.Ribbon.PageManager.get_instance();
 
         pm.add_ribbonInited(function() {
            DoSomethingWithRibbon();
         });
 
         var ribbon = null;
         try
         {
            ribbon = pm.get_ribbon();
         }
         catch (e) { }
 
         if (!ribbon) {
            if (typeof(_ribbonStartInit) == "function")
               _ribbonStartInit(_ribbon.initialTabId, false, null);
         }
         else {
            DoSomethingWithRibbon();
         }
      },
      "sp.ribbon.js");
   </script>
</asp:Content>

 

實用隱藏方法

function hideEditRibbon() {
           var ribbon = SP.Ribbon.PageManager.get_instance().get_ribbon();
           // Set the tab to the “Browse” tab
           SelectRibbonTab("Ribbon.Read", true);
           // Remove the “Edit” tab from a list from from the ribbon.
           ribbon.removeChild('Ribbon.ListForm.Edit');
     }

     SP.SOD.executeOrDelayUntilScriptLoaded(function() {

           var pm = SP.Ribbon.PageManager.get_instance();
     
           pm.add_ribbonInited(function() {
                hideEditRibbon();
           });

           var ribbon = null;
           try {
                ribbon = pm.get_ribbon();
           }
           catch (e) { }

           if (!ribbon) {
                if (typeof(_ribbonStartInit) == "function")
                      _ribbonStartInit(_ribbon.initialTabId, false, null);
           }
           else {
                hideEditRibbon();
           }
     },
     "sp.ribbon.js");

相關文章