I would like to check with JavaScript if the current logged user is in a specified and specific group (for example called "Admins Group").
How can I do?
Thanks
I would like to check with JavaScript if the current logged user is in a specified and specific group (for example called "Admins Group").
How can I do?
Thanks
How to check if current user belongs to SharePoint group via CSOM (JavaScript):
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);
var group = allGroups.getByName(groupName);
currentContext.load(group);
var groupUsers = group.get_users();
currentContext.load(groupUsers);
currentContext.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}
function OnFailure(sender, args) {
OnComplete(false);
}
}
Usage:
function IsCurrentUserHasContribPerms()
{
IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
// The current user is in the [Members] group!
}
});
}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');
Here's a quicker way with SharePoint 2013:
function CheckCurrentUserMembership() {
var clientContext = new SP.ClientContext.get_current();
this.currentUser = clientContext.get_web().get_currentUser();
clientContext.load(this.currentUser);
this.userGroups = this.currentUser.get_groups();
clientContext.load(this.userGroups);
clientContext.executeQueryAsync(OnQuerySucceeded);
}
function OnQuerySucceeded() {
var isMember = false;
var groupsEnumerator = this.userGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group= groupsEnumerator.get_current();
if(group.get_title() == "Administrator Group") {
isMember = true;
break;
}
}
OnResult(isMember);
}
function OnQueryFailed() {
OnResult(false);
}
clientContext.executeQueryAsync(OnQuerySucceeded.bind(this))
Commented
Jun 14, 2016 at 5:31
This code worked for me, its simple and does the job
var url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups'
$.getJSON(url, function (data) {
$.each(data.value, function (key, value) {
if (value.Title == 'GroupName') {
//*****your code goes here*****//
}
});
})
Function:
function isCurrentUserMemberOfGroup(groupName) {
var userIsInGroup = false;
$.ajax({
async: false,
headers: { "accept": "application/json; odata=verbose" },
method: "GET",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups",
success: function (data) {
data.d.results.forEach( function (value) {
if (value.Title == groupName) {
userIsInGroup = true;
}
});
},
error: function (response) {
console.log(response.status);
},
});
return userIsInGroup;
}
Usage:
var isAdmin = isCurrentUserMemberOfGroup("Admins Group");
Personnaly, the method with the SP.ClientContext didn't worked. So I used an asynchronous request to the Web API of SP like this :
$.getScript("/_layouts/15/SP.UserProfiles.js");
SP.SOD.executeOrDelayUntilScriptLoaded(checkAdminRights, "SP.UserProfiles.js");
function checkAdminRights() {
var userId = _spPageContextInfo.userId;
var groupId = 7;
var requestHeaders = { "accept" : "application/json; odata=verbose" };
$.ajax({
url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(" + groupId + ")/users/getbyid(" + userId + ")",
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : userAdmin,
error : userNotAdmin
});
function userAdmin(data, request){
// if we reach here, the current user belongs to the group Id 7 for the example
var userName = data.d.LoginName;
}
function userNotAdmin(error) {
console.log(error.textStatus);
}
}
For my example, I checked if current user belongs to the group of id 7 (sharepoint admin), if it's the case, ajax request is a success, then I do my process. If the user is not found in the group, function fails.