16

I'm trying to get the group of current user. By using this code:

function checkGroup() {
    var group = ["D", "Sup", "A", "CP"];
    for (var i = 0; i < group.length; i++) {
        if (isMember(group[i]) == true)
        {

            alert('I am a member of the group'+ i);
            break;
        }
    }

}    

function isMember(groupName){
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+groupName+"')/Users?$filter=Id eq " + _spPageContextInfo.userId,
        method: "GET",
    headers: { "Accept": "application/json; odata=verbose"},
    success: function (data) {
        alert('I am a member of the group' + groupName);
        return true;

    },
    error: function (data) {
        return false;

    },
        asyn:false,
    });
}

the problem is , it always return true , means i the member of all group which is false .

Any idea ??

  • You could've used this if you set async to false (an option inside $.ajax({ }) and returned a variable with true or false. I'm not recommending it though. – Dinerdo Jan 8 '16 at 1:40
19

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

  • Vadim, will this work if the current logged in user is in an Active Directory Group residing in a SharePoint Group? – Bismarck Jun 27 '16 at 12:59
  • 1
    @Bismarck, unfortunately no, AD Group is treated as a user in SharePoint and therefore AD Group needs to be "expanded" – Vadim Gremyachev Jun 27 '16 at 13:14
  • how to check whether the current logged in user belongs to a single or multiple group(s), where the groupname starts with "ABC" , like "ABC HR", "ABC Sales" , "ABC Finance" ,, etc.. – prasadwt Apr 10 '17 at 10:12
  • 2
    Thanks for the /_api/web/currentuser endpoint. Didn't know it. – GBU Jun 13 '17 at 18:12
  • Can one use '/_api/web/getuserbyid(' + userid + ')/?$expand=groups' to get the groups of any user? – Nicholas DiPiazza Sep 1 '17 at 12:31
0

You can get current Loggedin user's name using below code as well.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
    var userid = _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept": "application/json;odata=verbose" };
    $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: onSuccess,
        error: onError
    });

    function onSuccess(data, request) {
        var Logg = data.d;
        //get login name
        var loginName = Logg.LoginName.split('|')[1];
        alert(loginName);
        //get display name
        alert(Logg.Title);
    }

    function onError(error) {
        alert("error");
    }
</script>
  • thanks hardik , im not looking for the the current user name or login name , but the group of the current user ? – Imen Turki Apr 4 '15 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.