Tuesday, November 6, 2007

Hide "New" document menu item in document library views

Hi all,

Recently my customer asked me to do something I generally do not approve of - changing the basic behavior of a SharePoint document library.

I usually for maintaining a solid user experience and believe it is one of the many strong point SharePoint has to offer by giving the user a familiar environment throughout his LOB systems - if it’s a team workspace, an internal organization portal or (new in 2007) the organization internet portal.

Well, with all that said - one of my customers still insisted on removing the "new" document menu item from all the document libraries and allowing only to "upload" documents...

Like a good SharePoint-boy I immediately thought my solution will be creating a new feature that uses the tag for hiding different menu items in SharePoint.

To my surprise I learned that the “new” menu item in the list view web part tool bar is not listed as a in any feature, so it cannot be removed as so.

Finding no other choices, I created this JS code in a separate JS file and referenced it from all master pages involved…

If checks if the current page has a document library view and if so – locate and hide its “new” menu item…

Just add it to your page, and call HideNewDocumentMenuItem on body’s load:
function HideNewDocumentMenuItem()
{
try
{
if( ctx )
{
if( ctx.listBaseType == 1 )
{
var tables = document.getElementsByTagName("table");
for(var i=0; i< tables.length; i++)
{
if( tables[i].id.indexOf("NewMenu") > 0 )
{
var elm = tables[i];
elm.parentElement.parentElement.style.display="none";
elm.parentElement.parentElement.nextSibling.style.display="none";
}
}
}
}
}
catch(e){}
}


Hope this helps you guys,
If you have a more elegant solution – please post a comment… I’ll be happy to hear.

Shai Petel (Ben Shooshan).

12 comments:

Marie Wessels said...

Thanks! You just saved me another wasted day, i've been hunting through features like a nut.

Gladys said...

Hi!

If you set the Toolbar Type to be Summary Toolbar you get the same effect, only upload is allowed.

bornvirgo said...

Hi,
I am trying the upload option menu in document library. I created a content editor webpart and added the code. Its working when the page is in edit mode and not otherwise. Do you know what could be the issue? Please let me know. My email id is bornvirgo76@gmail.com.

Thanks,
Srinivas.

Aseem said...

Here is how you can remove sub menu items under New menu:

function HideNewDocumentMenuItem()
{
try
{
if (ctx)
{
if (ctx.listBaseType == 1)
{
var menuItems = document.getElementsByTagName("ie:menuitem");
for(var i=0; i< menuItems.length; i++)
{
if (
(menuItems[i].text == "Demo Material") ||
(menuItems[i].text == "Case Study")
)
{
var elm = menuItems[i];
elm.hidden=true;
}

}
}
}
}
catch(e)
{
alert(e.description);
}
}


HideNewDocumentMenuItem(); // call

Moderator said...

This is great, thanks all! Can someone show similar code for how to modify the context menus (e.g. id_editProperties)?

Thanks,
da

Shai Petel (Ben Shooshan) said...

Thanks Assem!!!
Nice code, I'll check it soon :)

Also - just for editing items menues you can have a look here:
http://www.kwizcom.com/ProductPage.asp?ProductID=32&ProductSubNodeID=141

we had something for 2003, try asking our sales at kwizcom.com about a newer version for 2007...

Joel said...

I use sharepoint designer to remove the tag directly.... do you think it is a problem, actually, my sharepoint application is quite a not very big app... so i think unghosting is not a concern for me...

And also, i hv a question, where should a place your javascript?? Also using sharepoint designer..?

Happy to discuss this and thanks.

Santha Baktha Shanmugam said...

I want to hide a menu from document library's context menu, but this logic seems not working.

Do we have any idea on this

Shai Petel said...

Hi Joel,

You can add it usign a content editor web part in the page using the browser. The whole point is not to edit in front page as it unghosts the page and reduce performance.

Santha, is this what you tried? If it is not working - what is the issue you are experiencing?

Kacper said...

And how to remove the "New" from the menu toolbar for a List?

Anonymous said...

Check for ctx.ListBaseType = 0
to apply to lists

Anonymous said...

Alternatively, just blank out the "document template" for the library (in advanced settings). This removes the "new document" menu option.