Hiding a field on a List Form

One of the more frustrating aspects of SharePoint is that permissions do not work like most people expect. Unlike a folder, where you can assign someone read access to the folder, read access to one file & edit access to another file, SharePoint access is across the board when it comes to list data. It’s more like a spreadsheet where you want someone to see & edit Columns A & C on each row but not Column B.

Anyway, one of the sites I admin for needed to hide specific buttons & fields on the Edit form for one group of users, while still allowing them to edit other fields on the form. What I ended up with was the following script that uses jQuery and SPServices to dynamically hide an item based on group membership. While it doesn’t address the larger issue of a user having edit access to the entire list item (they could still get to the fields via the datasheet view, for instance), it DOES allow you to hide specific items on the list form according to what groups the user is in.

The master page for the site collection already has references to jQuery and SPServices in it, so I simply opened EditForm.aspx in SharePoint Designer and added the following:

$(document).ready(function() {
$().SPServices({
operation: “GetGroupCollectionFromUser”,
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
if($(xData.responseXML).find(“Group[Name=’Engineering’]”).length == 1)
{
// alert(“You shouldnt see the button.”);
$(“input[id=’AcctBtn’]”).hide();
}
}
});
});

Now, when a user from our Engineering department opens the Edit form for a list item, they no longer see the “Accounting Info” button at the top of the screen. It’s not a perfect solution by any means, but it does give us some granularity at the form level – a feature which is sorely lacking in vanilla SharePoint. This same technique can be used to hide individual fields on the form, or pretty much any other element on the page.

BTW, thanks to Marc Anderson (sympmarc.com) and team for the awesome SPServices library. It’s saved my bacon on multiple occasions.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s