Solution:
Use SPServices to check whether the current user is member of a particular group. If not, hide the field using jQuery (or you can make the field Read-only too).
<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<!-- Download SPServices from: http://spservices.codeplex.com/ Or use this CDN -->
<script type="text/javascript" src="http://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.
if($(xData.responseXML).find("Group[Name='Vertical Owners']").length != 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>
Using REST API:
You can also use the SharePoint REST API to achieve the same:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
IsCurrentUserMemberOfGroup = function (groupName) {
var result = false;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups',
type: "GET",
async: false,
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function(data){
var groups = data.d.results
for(i=0; i<groups.length; i++) {
if (groups[i].LoginName == groupName) {
result = true;
}
};
},
error: function() { return false; }
})
return result;
}
checkUser = function() {
if (IsCurrentUserMemberOfGroup("Template Test Members") == false)
$('nobr:contains("Feedback")').closest('tr').hide();
}
_spBodyOnLoadFunctionNames.push("checkUser");
</script>
To make fields read-only:
$("#fieldName").attr('readonly','readonly');
For choice field:
$("select[title^='Field Name']").attr('readonly', 'readonly');
Reference
- Hide SharePoint List Columns based on User Permissions.