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
June 30, 2011 at 3:35 pm |
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
June 30, 2011 at 6:12 pm
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
June 30, 2011 at 6:32 pm
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
June 30, 2011 at 8:28 pm
Hi,
Look at this comment regarding something similar:
http://sharepointjavascript.wordpress.com/2009/11/05/requests/comment-page-2/#comment-678
Alexander
June 30, 2011 at 8:53 pm
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
June 30, 2011 at 9:06 pm
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
June 30, 2011 at 11:05 pm
Sorry, I didn’t reply in the right way.
The solution works fine like that, thank you a lot
Marc
June 30, 2011 at 10:53 pm |
Thanks a lot Alexander, the redirection script is OK.
Marc
July 4, 2011 at 2:11 pm |
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
July 5, 2011 at 7:18 pm
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
July 6, 2011 at 8:47 am
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
July 12, 2011 at 4:09 pm |
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.
July 12, 2011 at 7:26 pm
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
July 12, 2011 at 7:39 pm
You’re the bomb Alexander, it worked perfectly!
November 12, 2011 at 8:24 am |
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?
November 30, 2011 at 11:23 pm
SharePoint 2007 or 2010?
Alexander
January 19, 2012 at 10:30 pm |
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. >:(
January 19, 2012 at 10:41 pm
Try using jQuery v1.6.4. I guess that version should work.
Alexander
January 19, 2012 at 11:50 pm
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.
March 6, 2012 at 7:18 pm |
FYI – had to return to jquery 1.3.2 to make this script work. Thanks!
April 3, 2012 at 9:58 pm |
Is there a 2010 version of this code available?
Thanks.
April 9, 2012 at 4:36 pm |
Alexander, could you please adopt for SP2010? Thank you very much !
April 23, 2012 at 7:28 am |
view…
[...]Hide menu items in list view toolbar and views in view selector « SharePoint JavaScripts[...]…