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).

21 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.

Anonymous said...

try this, it is more simple !!!

(great than symbol ) style type="text/css"
.ms-splitbutton {display: none;} /*MOSS*/


/style (less than symbol)

***Added in an Content Editor Web part (hidden)








from http://www.heathersolomon.com/content/sp07cssreference.htm

Anonymous said...

unfortunately, the ms-splitbutton technique removes the upload button as well (which is not what we wanted).

Beetlejuice said...

Thank you. I just used this and it worked great.

JShidell said...

Thanks for your tip. I couldn't get yours to work for some reason though. The way I got mine to work was with a little bit of JQuery. For those that are interested.

$(document).ready(function(){
$('.ms-menutoolbar td:lt(4)').hide();
});

hopefuly this helps those that are having some problems.

Make sure you reference the JQuery library

Tip: Depending on if you are trying to hide the "new button" on lists or document libraries you might have to change the number location within lt(). I had to use $('.ms-menutoolbar td:lt(2)').hide(); instead of (4) to get mine to work within my document library.

Anonymous said...

Just once, just once I'd like a blogger/developer to say where we put the code on the page and where to call it from. Simply saying just place this on your page and call it from the the body load doesn't help an aweful lot of people!!

Shai Petel said...

Hey Annony :)

I have to say it never occured to me to mention it!

The most simple way in SharePoint to do it is to just add a content-editor web part, edit the HTML source, and put the script between <script>
-- code goes here ---
</script>

Now, you can also include it in your web part, custom control, master page, or external JS - this is why developers don't mention it, since each of the above may be suitable on different solutions.

Now, to call the method in "onload" (which will probably be your next question) is kind of tricky if you don't have control over the master page.
So just add this to the end of the script and it should work:
window.serTimeout(HideNewDocumentMenuItem, 100);

It's kind of a hack, but it will do the job.

Good luck!

Anonymous said...

Works great. Unfortunately, this doesn't work with a "Calendar View" of a document library.

The Calendar View does not use a ctx object. :(
-Joe

Anonymous said...

I found out this code doesn't work with a "Calendar View" of a document library, since a Calendar view does not have a ctx object.

The fix to work for all views appears to be simply leaving out the ctx check:

try
{
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){}


~Joe

Shai Petel said...

Gee, Thanks Joe!
Actually, any view that does not have the context menu dropdown will not have CTX object.
perhaps checking if it is there first and if not - use a code similar to yours would do the trick.