Hide SharePoint List Columns based on User Permissions
Requirement: We have a “Requests” link in our Help Desk site. The “Status” field in the help desk requests list should be hidden when users create a new entry in the list. Same time, Status field must be visible to people in “Help desk operators” group.
So, the requirement is to hide the SharePoint List Form field based on user permissions!
Solution: How to hide SharePoint list columns based on user permissions?
Use SPServices to check whether the current user is a member of a particular group. If not, hide the field using jQuery (or you can make the field Read-only too: How to Make SharePoint List Column Read-Only). Here are the detailed steps:
- Place the below script in a text file, upload to any SharePoint library of the site.
- Edit the NewForm.aspx, Add a content editor web part just below form fields, point the script file in CEWP and make the content editor web part hidden.
<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<!-- Download SPServices from: https://sympmarc.github.io/SPServices/ Or use this CDN -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
var xml = xData.responseXML.xml;
//If the current User does belong to the group "Service desk Operators"
if (xml.search('Service Desk Operators') == -1)
{
// alert("No, User Doesn't Exists!");
$("select[title=Status]").parent().parent().parent().hide();
//or use: $('td.ms-formlabel:contains("Status")').parent().hide();
//You can also use: $('nobr:contains("Status")').closest('tr').hide();
}
}
});
});
</script>
Instead of content editor web part, You can also edit the NewForm.aspx or EditForm.aspx file and place the code under “PlaceHolderAdditionalPageHead” to hide fields in SharePoint list forms.
Here is the NewForm.aspx view for “Help Desk Operators” – Note that the “Status” field is visible.
and here is the view for end-users: SharePoint list columns hidden based user permissions.
Does this script work for SharePoint Online as well
Hi,
Thanks for the jQuery.
I am getting error at completefunc: function (xData, Status), could you please help me in resolving the issue.
Hi Salaudeen Rajack,
Thank you for this script.
It is very good and it is workin correctly on IE.
I have two questions:
1. Is there any chance to make fileld readonly instead of hidding it?
2. Why this scrip does not work at all in other browsers?
Thanks you once again!
Great script!
Hello Salaudeen,
Is there a way users can only see certain company, regardless of site permissions or list permissions? first i merged various lists in one list with same columns, for example: company 1 list, company 2 list, ….company 10 list etc. Let’s say all those company list has common column called company number. Then i grouped according to company number.
Now, can we let only certain users view/open certain groups?
Please help.
Thanks for the workaround. However, My field doesn’t hide even though I am not member of the group. I tried putting alert() on xData (coming as “object Object” error) and xml (coming as “undefined” error). I am referring to the correct path for both *.js files. Am I doing anything wrong?
its not working in other browsers ,
can you please provide the solution
not working in chrome or mozilla
Does this work with SharePoint 2013?
Yes! It works on SharePoint 2013 also.
Hi Paul, Jquey works only when user is directly added to the SharePoint group. It doesn’t work when user is added to AD security group and this security group is added to SharePoint group.
Did you ever encounter this issue?
Regards,
Khushi
Hi,
Running the above code gets this error: Uncaught TypeError: Cannot call method ‘search’ of undefined
Any ideas why? The error is on this line of code: if (xml.search(‘PUBLISHERS’) == -1)
SPServices.js pulled form a Doc Lib in my site collection.
*PUBLISHERS is the name of my group*
Full code (All “>” pulled out of the Script tags to pass the comment validation:
script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js”>/script
scipt type=”text/javascript” src=”../../SiteAssets/Scripts/jquery_SPServices-2013_01.min.js”>/sript
script type=”text/javascript”
$(document).ready(function() {
$().SPServices({
operation: “GetGroupCollectionFromUser”,
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
var xml = xData.responseXML.xml;
//If the current User does belong to the group “Service desk Operators”
if (xml.search(‘PUBLISHERS’) == -1)
{
// alert(“No, User Doesn’t Exists!”);
$(“select[title=ViewBy]”).parent().parent().parent().hide();
//or use: $(‘td.ms-formlabel:contains(“Status”)’).parent().hide();
//You can also use: $(‘nobr:contains(“Status”)’).closest(‘tr’).hide();
}
}
});
});
/script
thanks,
Jon
Jon,
Just validate the file paths of the referenced js files ( SPServices.js & jQuery.js). Not sure that the new versions of SPServices undergone changes. Try using the same version as in my code.
Hi
Thanks for providing the script.
The IF command was not working for me as well. Jon, have you resolved this error?
Salaudeen, could you please help me on this.
One of the lines in above code is incorrect. Replace the below line and your code will work.
completefunc: function (xData, Status) {
if (($(xData.responseXML).find(“Group[Name=’YourGroupName’]”).length == -1))
……