Ram Prasad
 

SharePoint JSOM code to Hide/Remove Ribbon tabs in SharePoint List Forms or Pages

Jun, 20 2016
 
1 min/s
 
 

The Ribbon shows certain tabs like 'Edit', 'View', 'Items', 'List' etc., when we are on a list form page or when a List is added to any SharePoint page. In some cases we do not want to show these tabs to the users, which can be hidden by adding JSOM code to remove the unwanted tabs. Below is the code to get an instance of the Ribbon and remove the Edit tab from the Ribbon. This JavaScript can be added to a certain list form page through a Script Editor WebPart or can be included in a custom list definition.

 <script type="text/javascript">
        function hideEditRibbon() {
            try {
                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');
            } catch (ex) {
        }
    }
  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"); </script>

Below is the list of valid Ribbon tabs which can be hidden/removed by passing this value to the ribbon.removeChild method in the above snippet.

  • Ribbon.BDCAdmin
  • Ribbon.DocLibListForm.Edit
  • Ribbon.ListForm.Display
  • Ribbon.ListForm.Edit
  • Ribbon.PostListForm.Edit
  • Ribbon.SvcApp
  • Ribbon.Solution
  • Ribbon.UsageReport
  • Ribbon.WikiPageTab
  • Ribbon.PublishTab
  • Ribbon.WebPartPage
  • Ribbon.WebApp
  • Ribbon.SiteCollections
  • Ribbon.ManageTrust
  • Ribbon.EditingTools.CPEditTab
  • Ribbon.EditingTools.CPInsert
  • Ribbon.Image.Image
  • Ribbon.Document
  • Ribbon.Library
  • Ribbon.ListItem
  • Ribbon.List
  • Ribbon.Link.Link
  • Ribbon.Table.Layout
  • Ribbon.Table.Design
  • Ribbon.WebPartInsert.Tab
  • Ribbon.WebPartOption
  • Ribbon.Calendar.Events
  • Ribbon.Calendar.Calendar
  • Ribbon.Permission