Sunday, September 16, 2012

Hide SharePoint 2007 List Toolbar buttons and Menu Items

Got a requirement to Hide SharePoint toolbar buttons (E.g. "New", "Upload"), Menu Items (E.g. "Upload Multiple Files", "Alert Me") from all document libraries of a SharePoint 2007 site collection.

  • If its is for just a single document library, this can be achieved by wring Javascript-CSS, place it in Content Editor web part to hide the "New" button.
  • If its for the entire Farm, this can be achieved by Override the ToolbarActionsMenu template: http://blogs.msdn.com/b/dipper/archive/2006/10/05/how-to-remove-or-hiding-items-in-list-toolbar-in-sharepoint-server-2007.aspx
  • HideCustomAction? Nope, ListViewWebPart menu items are rendered as a web control from the Microsoft.SharePoint.dll, So they can't be hidden through the "HideCustomAction" feature.
Hide "New" button from SharePoint 2007 List Toolbar

Solution:
jQuery or JavaScript with Delegate Control. How? The overall idea is, plug the jQuery to hide "New" button into "AdditionalPageHead" delegate control of the Master page.

Just Create a WSP Builder project and add a feature with out receiver. Add a User control (say, HideNewMenu.ascx) 
jQuery code to Hide List toolbar Buttons

Hide New Button from SharePoint List Toolbar
Code for HideNewMenu.ascx
<%@ Control Language="C#" %> 

<script type="text/javascript" src="/_layouts/jQuery/jquery-1.4.1.min.js"></script>
<script>
    $(document).ready(function()
      {
   if(ctx)
      {
                if(ctx.listBaseType == 1)
             {
                      $('.ms-menutoolbar td:lt(4)').hide();
                    }
             }
     });
</script> 

Same way, You can hide any Tool bar buttons. E.g. Hide "Upload" button in Toolbar. Just get into the view source of the page to fetch the target.

<script type="text/javascript" src="/_layouts/jQuery/jquery-1.4.1.min.js"></script>
<script>
    $(document).ready(function()
      {
$('.ms-menutoolbar td:eq(3)').hide();
$('.ms-menutoolbar td:eq(4)').hide();
    });
</script>

Nothing special in Feature.xml, but in Elements.xml update the code so that it applies the delegation on "AdditonalPageHead".

Elements.xml code:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control
    ControlSrc="~/_controltemplates/HideNewMenu.ascx"
    Sequence="90"
    Id="AdditionalPageHead">
  </Control>
</Elements>


Build and deploy the wsp, Go to Site collection features and activate the feature (I've named it as "Hide New button in Document Library Toolbar") and See it in action in any document library of the site collection.
"New" List toolbar button Hidden

Similarly, If you want to hide individual menu items, you can use the JavaScript from Ayman: http://www.codeproject.com/Articles/32622/SharePoint-Customization-Tricks-Part-1.

If you don't have jQuery and want to achieve the same with JavaScript:
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("hideNewMenuToolbar()");

function hideNewMenuToolbar()
{
  try
  {
 if(ctx)
 {
 if( ctx.listBaseType == 1 ) //Making sure its a doc lib
 {
 var aTags=document.getElementsByTagName('a');

           for(i=0; i < aTags.length; i++)
            {
               if(aTags[i].id.indexOf("NewMenu")!=-1)  
                 {
                   aTags[i].parentNode.parentNode.style.display='none';
                 }
            }
        }
        }
   }
 catch(e){}
}

</script>

Hide Toolbar Menu Items using jQuery:
Say for E.g. Lets hide the "New Document" Menu item under "New" menu of the document library toolbar:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>

<script>
    $(document).ready(function()
      {
   if(ctx)
      {
                if(ctx.listBaseType == 1)
             {
                     $("ie\\:menuitem[text='New Document']").attr('hidden', true);
                   }
             }
     });
</script>

How to Hide Toolbar Menus using JavaScript:
To hide an individual Menu items: say, Hide "New Document" from New button's context Menu in document libraries: You can use MultipleUpload in place of New0 to hide "Multiple File Upload".

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("hideToolbarItem()");

 function hideToolbarItem()
{
  var doc = document.getElementsByTagName('ie:menuitem'); 

  for (var i = 0; i < doc.length; i++)
  {
    itm = doc[i];    
     if (itm.id.match('New0')!=null) 
        { 
           itm.hidden=true; 
        }
  } 
}
</script>



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Check out these SharePoint products:

1 comment :

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...