The following ECMAScript checks, whether the current logged in user belongs to particular group.
In my case there was a scenario, where administrator was able to see all fields on form, while current user was able to see limited set of fields.
Add this script on that page or create JS file and refer in page, where you want to check, if current user is in particular group
<script type=”text/javascript”>
//this make sure the function is called once the page is loaded completely
_spBodyOnLoadFunctionNames.push(onPageLoad());
function onPageLoad() {
ExecuteOrDelayUntilScriptLoaded(checkUser, “sp.js”)
}
var adminUsers;
var vCurrUserName;
function checkUser() {
var currentContext = new SP.ClientContext();
//get current user
vCurrUserName = currentContext.get_web().get_currentUser();
currentContext.load(vCurrUserName);
// get Admin group
var groupCollection = currentContext.get_web().get_siteGroups();
var _group = groupCollection.getById(10); // ID of the Group
adminUsers = _group.get_users();
currentContext.load(adminUsers);
currentContext.executeQueryAsync(Function.createDelegate(this, this.checkUserSuccess), Function.createDelegate(this, this.checkUserFailure));
}
function checkUserSuccess() {
var vIsAdmin = false;
var listEnumerator = adminUsers.getEnumerator();
while (listEnumerator.moveNext()) {
var item = listEnumerator.get_current();
//check current user login name and user login name from admin group
if (vCurrUserName.get_loginName() == item.get_loginName()) {
vIsAdmin = true;
break;
}
}
if (vIsAdmin) {
//Write code to show fields
if (document.getElementById(‘CheckBoxColID’) != null) {
document.getElementById(‘CheckBoxColID’).style.display = “block”;
}
}
else {
//Write code to hide fields
if (document.getElementById(‘CheckBoxColID’) != null) {
document.getElementById(‘CheckBoxColID’).style.display = “none”;
}
}
}
function checkUserFailure() {
//Failed to get user. This function will execute only if the user doesn’t belong to admin group or doesn’t have permission to view members from admin group
//Write code to hide fields
if (document.getElementById(‘CheckBoxColID’) != null) {
document.getElementById(‘CheckBoxColID’).style.display = “none”;
}
}
</script>