Hide menu items in list view toolbar and views in view selector


This is a short one on how to remove menu items from the “New”, “Actions”, “Settings”, and “List view’s” menu in a list or document library.

This method requires a CEWP added below the list view web part with the code, and is a “per view code” which must be added to every view where the menu items shall be removed.

I will first give a list of the “names” of the “standard” elements found in the list view toolbar menu and list view menu, then i will give an example of how to remove selected items from the menu’s.

The menu items in the “Actions”, and the “Settings” menu has these “names” (the part of the ID used to locate them):

  • _EditInGridButton
  • _ExportToSpreadsheet
  • _ExportToDatabase
  • _ViewRSS
  • _SubscribeButton
  • _AddColumn
  • _AddView
  • _ListSettings

The “New” menu (the one containing all the “New-button” for all your content types) uses “_New0″ for the first “_New1″ for the next and so on.

The “view’s” menu uses these names for the “standard” menu items:

  • _DefaultView
  • _ModifyView
  • _CreateView

The custom made views uses the “DisplayName” of the view to identify it.

Code for hiding elements in “New”, “Actions”, “Settings” and “View’s” menu:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
/* Hide menu items in list view toolbar and views in view selector
 * ---------------------------------------------
 * Created by Alexander Bautz
 * alexander.bautz@gmail.com
 * http://sharepointjavascript.wordpress.com
 * v1.0
 * LastMod: 26.11.2009
 * ---------------------------------------------
 * Include reference to:
 *  jquery - http://jquery.com
 * ---------------------------------------------
*/

// Remove menu items - in this example all items in the "Settings" menu are removed (and therefore the menu is removed)
// This is the array of menu items to hide
var arrOfMenuItemsToHide = ['_EditInGridButton','_AddView','_AddColumn','_ListSettings'];
$.each(arrOfMenuItemsToHide,function(idx,item){	
	$("*[id$='" + item + "']").next().andSelf().remove();
});

// Views - hides "Modify", "Create" and the custom made "My Test View"
// This is the array of view's or to hide
var arrOfViewsToHide = ['_ModifyView','_CreateView','My Test View'];
$.each(arrOfViewsToHide,function(idx,item){	
	$.each($("*[id$='_ViewSelectorMenu']").children(),function(){
		if($(this).attr('id').match(item) || $(this).attr('text')==item){
			$(this).next().andSelf().remove();
		}
	});
});

// Remove the menu if all menu items are removed
$(".ms-menutoolbar").find('menu').each(function(){
	if($(this).children().length==0){
		$(this).parents('td.ms-toolbar:first').prev().andSelf().remove();
	}
});
</script>

The jQuery-library is found here. The sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.

This code can be adapted to hide elements based on group membership of the logged in user. I have written about a method to get the group collection for a user and to verify group membership here Showing or hiding list fields based on membership in a SharePoint group.

Ask if something is unclear.

Regards
Alexander

66 Responses to “Hide menu items in list view toolbar and views in view selector”

  1. Marc Says:

    Hello Alexandre,

    Great Blog ! Very useful !!
    I’m able to hide some views in the view selector with the above code. Here is an easy one I hope. How do I select a specific view among those which are left ? Or modify the default view ?

    thanks

    • Alexander Bautz Says:

      Hi, I’m not quite sure what you mean. If you want to hide a “user made” view you use it’s display name.

      Alexander

    • Marc Says:

      Let’s say after removing some views I have left : ‘_CreateView’, ‘TestView1′ (which is the default view) and ‘TestView2′.
      Depending on who is logged in I would like to display the list with ‘TestView2′. I followed your other guidelines for user ID identification and it works. But how can I force/select with javascript ‘TestView2′ ?
      I hope it’s clearer now
      thanks in advance

    • Alexander Bautz Says:

      Hi,
      Look at this comment regarding something similar:
      http://sharepointjavascript.wordpress.com/2009/11/05/requests/comment-page-2/#comment-678

      Alexander

    • Marc Says:

      Thanks for your patience and the suggestion, however my url is static. If I take your code, what I’m looking for is more a piece that would imitate something like :
      var viewToShow = ['TestView2'];
      $.each(viewToShow,function(idx,item) {
      $.each($(“*[id$='_ViewSelectorMenu']“).children(),function() {
      if($(this).attr(‘id’).match(item) || $(this).attr(‘text’)==item) {
      $(this).next().andSelf().?selectView?();
      }
      });
      });

      I guess the code could be written with just one line, I don’t need a loop obviously for the view to display is unique. Is this possible ?
      Marc

    • Alexander Bautz Says:

      If you want to redirect the user to “TestView2″ you could just redirect to the viewURL – there is no need to get it from the view selector – just use javascript and write: location.href=L_Menu_BaseUrl+”/”+TestView2.aspx

      Or am i missing something?

      Alexander

    • Marc Says:

      Sorry, I didn’t reply in the right way.
      The solution works fine like that, thank you a lot
      Marc

  2. Marc Says:

    Thanks a lot Alexander, the redirection script is OK.
    Marc

  3. Marc Says:

    Hiding View’s items is working fine in IE, but not in Firefox 3.x. Does anybody know what needs to be changed in the JS to get this functionality also working in Firefox ? Is it actually possible ?
    thanks in advance

    • Alexander Bautz Says:

      Hi,
      I have tested it in Firefox 5 and it works. I do not have access to Firefox 3.x at the time, but it probably has to do with Firefox 3.x handling “children” in a different way. Try alerting the “children.length” and see what you can find.

      Alexander

    • Marc Says:

      Thanks Alexander,

      Not sure what I did wrong, but anyhow I got it finally running on Firefox 3.x with the original scripts.

      regards

      Marc

  4. JeffG Says:

    Hi Alexander,
    Do you know of any jquery scripts to hide menu item(s) on the item display form? I am in a situation where I have to give a group of people rights to edit items, but I do not want them starting or cancelling workflows manually, so I would like to hid that menu option when the display the item. Any help would be appreciated.

    • Alexander Bautz Says:

      Hi,
      The approach is a bit different between SP2010 and SP2007

      For English language SP2007 use something like this:

      $("table.ms-toolbar").find("a[title='Workflows']").parents('table:first').parent().next().andSelf().remove();
      

      And for SP2010 something like this:

      $("#s4-ribboncont").find("a[id*='Workflows']").parents('span.ms-cui-row').remove();
      

      Alexander

    • JeffG Says:

      You’re the bomb Alexander, it worked perfectly!

  5. Tony Says:

    I see how you removed the control but what if I wanted to add a button after the work flow button how would I do it?

  6. bnw Says:

    I cannot get this to work on the views. Even straight cutting and pasting works on the settings menu but the views remain unchanged (Modify, Create, and My Test View, all still show.)

    I am on MOSS 2007 and jquery 1.7.1 and IE9. Any ideas? This script would be a godsend if I could get it to work. >:(

    • Alexander Bautz Says:

      Try using jQuery v1.6.4. I guess that version should work.

      Alexander

    • bnw Says:

      Thanks for the reply. Looks like its something with the browser. Chrome/Firefox hides the Create and Modify views correctly (misses ‘My Test View’.) IE9 seems to miss hiding all the views.

  7. Matt B Says:

    FYI – had to return to jquery 1.3.2 to make this script work. Thanks!

  8. Robin Says:

    Is there a 2010 version of this code available?

    Thanks.

  9. Alex Says:

    Alexander, could you please adopt for SP2010? Thank you very much !

  10. view Says:

    view…

    [...]Hide menu items in list view toolbar and views in view selector « SharePoint JavaScripts[...]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers