Pages

Tuesday, April 26, 2011

Hide Sub Menus in Actions Tab – SharePoint: Part 1

Few months ago, I was asked, "Is it possible to restrict the users (even with sufficient privileges to add, edit, delete items) from Editing, adding, deleting the items using 'Edit in DataSheet' option" because the application had a Custom Form for Add and Edit and those forms had some validations. It is very obvious that with Edit in DataSheet Form all it prompts you for are very few validations such as Required Field, DataType etc, but does not support validation depending on two colums.


I felt this would make a very interesting post. Also, I believe this is a very common requirement many customers would like to have it. My first option as always (as most of them would expect as per my previous posts) was to have a javascript fucntion to implement this.



  • Add a Content Editor WebPart to the Lists "All Items.aspx" page, where you would have the option "Edit in DataSheet", Export to Spread Sheet" etc under Actions Tab.

  • In the Content Editor WebPart Click Edit --> Modify Shared WebPart --> Source Editor --- Paste the below Script.


<html>
<head>
<script type="text/javascript" language="javascript">
HideDataSheet();
function HideDataSheet()
{
var vMenu = document.getElementsByTagName('menu');
for(var k = 0; k < vMenu.length; k++)
{
var vMenuItem = vMenu[k].getElementsByTagName('ie:menuitem');
for(var i = 0; i < vMenuItem.length; i++)
{
if(vMenuItem[i].getAttribute("text") == "Edit in Datasheet" || vMenuItem[i].getAttribute("text") == "Export to Spreadsheet")
{
vMenuItem[i].setAttribute("hidden",true);
}
}
}
}
</script>
</head>
<body>
</body>
</html>



  • Save the Content and click OK.


Now When you click on Actions Tab you will observe that the two options "Export to Spread Sheet", "Edit in DataSheet" no longer appear.


This script is tested across different browsers: IE, Firefox, Chrome and it works as expected. However, Please note that you wouldn't be seeing the Option "Edit in DataSheet" in browsers other than IE even without the above script as there is a default script which checks if the browser is IE or not.


Similarly, this script can be used to hide any other Sub menus. Please make sure that you replace the Title you are looking for with the one marked in Red.


A few more queries and its resolution with respect to this feature in my next Post.


Thank you!