1

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);
    }
}
2
  • Just trying to understand better, you need to check if the user is in an AD group, that has been assigned to the SP group?
    – WhiteHat
    Commented Jul 31, 2015 at 16:40
  • This has been asked several times, the short answer is you can't, there is just no coverage into AD for it. Here is an example of a work around, sharepoint.stackexchange.com/questions/134509/… Commented Jul 31, 2015 at 16:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.