I have a Sharepoint hosted app and I need to check if the current user is in a sharepoint security group no matter if this is in a AD inside the group, the following code check if the user is in the security group but only if he are explicit in the security group, the code does not look inside group that are in the security group.
function getItems() {
// only execute this function if the script has been loaded
if (ready) {
// the url to use for the REST call.
var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
"/web/lists/getbytitle('" + TituloListaEventos + "')/items?$select=Title,Category,StartDate,EndDate,EncodedAbsUrl,ID,LinkSolicitud&$filter=Category eq '" + Categoria + "'" +
"&@target='" + SPHostUrl + "'";
// create new executor passing it the url created previously
var executor = new SP.RequestExecutor(SPAppWebUrl);
// execute the request, this is similar although not the same as a standard AJAX request
executor.executeAsync(
{
url: encodeURI(url),
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
IsCurrentUserMemberOfGroup("Solicitantes", function (isSolicitante) {
...
},
error: function (data) {
...
}
});
}
}
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);
}
}