0

I am using below rest api call to check if user exist in Admin group but how to check if user exist in more then one group ex Admin and Member group

url: absoluteUri + "/_api/web/sitegroups/getByName('Admin ')/Users?$filter=Id eq " + _spPageContextInfo.userId,

3 Answers 3

0

You could either do a foreach-statement to check multiple groups (one at the time) or to it the other way around - get a users group membership and then loop through the results.

The user Vadim Gremyachev has added an excellent answer of to handle this here: Get the group of the current user using REST API

I'm quoting his full answer here:

The following example demonstrates how to determine whether current users belongs to the specified groups:

getCurrentUserWithDetails()
.done(function(data)
{
    var groupNames = ['News Owners','Approvers'];
    //determine wether current user is a memeber of group(s) 
    var userGroups = data.d.Groups.results;
    var foundGroups = userGroups.filter(function(g){ return groupNames.indexOf(g.LoginName) > -1});

})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

where getCurrentUserWithDetails function is used to get current user with groups:

function getCurrentUserWithDetails()
{
    var endpointUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';
    return $.ajax({
            url: endpointUrl,
            method: "GET",
            contentType: "application/json;odata=verbose",
            headers: {   
             "Accept": "application/json;odata=verbose"
            } 
        });
}

Key points: more optimal from performance perspective since only a single request is submitted to the server

0

This can be easily achieved through graph Api.

if you want to lists the group which user is member of.

GET https://graph.microsoft.com/v1.0/users/{id}/memberOf

if you want get count of group a user is member

GET https://graph.microsoft.com/v1.0/users/{id}/memberOf/$count

You can find further details on this referring below Microsoft docs

0

Can be acheaved by REST API too,

get the all groups that user belongs to:

"/_api/web/GetUserById(" + userId + ")/Groups"

in function:

GetUserGroups = function(userId){
    var Id = _spPageContextInfo.userId
    if (typeof userId != 'undefined' && userId!=""){
        Id = userId;
    }
    var result = [];
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + Id + ")/Groups",  
        method: "GET",   
        async:false,
        headers: {
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json;odata=verbose",
        },
        success: function(data){
            result = data.d.results
        }
    })
    return result;
}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.