A question I've been asked a couple of times recently is 'how to hide menu items on the list toolbars'
The problem faced by the clients was to hide the 'Edit in DataSheet' and 'Open in Explorer' commands.
The first attempt was to use a HideCustomAction element in a feature, this didnt work, so we had to look for an alternative solution.
Looking on the web provided two approached. Scott Hillier has a web part that can be placed on the page whcih will achieve the trick, but this would require modifying each existing library with the web part, plus ensuring that future library templates had the webpart definition applied.
Dipper Park has an alternative approach usig a custom control to hide complete sections of the menu. This was closer as it allowed all lists / libraries to be controlled.
In the end, we took the second option and created a custom control. The control contains a section of javascript to inspect the page objects & remove the individual menu items.
The script block was relatively simple & could be improved in future....
[code:js]
window.onload = HideMenuItem;
function HideMenuItem()
{
var menuNodes = document.getElementsByTagName("menu");
var targetNode;
var targetViewNode;
var viewExplorerNode;
var dataSheetNode;
var explorerNode;
for (var i = 0; i < menuNodes.length; i++)
{
var menuNode = menuNodes[i];
if (menuNode.childNodes.length > 3)
{
if (menuNode.childNodes[0].id.indexOf("EditInGridButton") != -1)
{
targetNode = menuNode;
dataSheetNode = menuNode.childNodes[0]; //is always node 0
explorerNode = menuNode.childNodes[2]; //is always node 2
}
if (menuNode.childNodes[0].id.indexOf("DefaultView") != -1)
{
targetViewNode = menuNode;
viewExplorerNode = menuNode.childNodes[2];
}
}
}
targetNode.removeChild(explorerNode);
targetNode.removeChild(dataSheetNode);
targetViewNode.removeChild(viewExplorerNode);
[/code]
After the script has run, the menus would look as per the following two images
It's important to note that in the above example, we are removing childNodes 0 & 2, really we should probably be checking for the location, rather than assuming they are 0 & 2
This could be taken a step further and wrapped into a feature. If a feature were to add some HTML onto the page (e.g. another menu item such as 'Print List') then the script could check for the existance of this menu item, then remove the childNodes.
The complete .ascx control is in the attachment
HideToolbars.zip (1.06 kb)