3

How can I check if the current user is a member of a specific SP Group using REST API and async/await?

_spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + grpName + "')/CanCurrentUserViewMembership";

here's my code but it doesn't work and want to use async/await but how?

$(document).ready(function() {
    ExecuteOrDelayUntilScriptLoaded(showAlert, "sp.js");
});

function showAlert() {

    if(isMember('Viewers') && !isMember('Owners'))
        alert('Viewers'); //current user is part of the Viewers group
}

function isMember(grpName) {
    var _ismember = false;
    var siteUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + grpName + "')/CanCurrentUserViewMembership";

    $.ajax({
        url: siteUrl,
        method: "GET",
        headers: { "accept": "application/json;odata=verbose"},
        success: function (data) {
            console.log(data);
            if(data.d.CanCurrentUserViewMembership != null) {
                return data.d.CanCurrentUserViewMembership;

            }   
        },
        error: function (error) {}
    });
}

UPDATE: ok so I managed to convert this to async/await, my next question is, how can I compare the two data returned (true/false) by the API?

isMember('Viewers')
        .then(data => console.log(data.d.CanCurrentUserViewMembership + ' viewers'))
        .then(
            isMember('Owners')
            .then(data => console.log(data.d.CanCurrentUserViewMembership + ' owners')))

what I want to do is if Viewers = true; and Owners = false, then I will show an alert (because I want to only show an alert if the user is a member of the Viewers group only, but if a user is a member of Viewers AND Owners, no alert should be shown)

4
  • If group do not have settings that everyone can see the members, then if your user is not member, code will give you error. Instead try to get the user's group and in loop or filter check if your required group is there. Apr 16, 2019 at 3:18
  • yes i've set this up already, i just need to have the code for async / await thanks
    – Aventus
    Apr 16, 2019 at 3:38
  • it seems it always returns true
    – Aventus
    Apr 17, 2019 at 2:37
  • Thanks this one works with Membership view able: Everyone. _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+ groupName +"')/Users?$filter=Id eq " + _spPageContextInfo.userId,
    – Sabeeh
    Apr 15, 2020 at 12:37

2 Answers 2

3

If I am not wrong, you want to call the isMember function twice and parallely. After both call executes, you want to check if user's membership.

Solution:

  1. If you are using jQuery:

    You can use jQuery.when().

    Reference: jQuery.when()

  2. If you are using Angular JS:

    You can use $q.all().

    Reference: $q.all()


Update: Use below code to check the membership of SharePoint group:

<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript" ></script>

<script type="text/javascript">
$(function(){
    isMember("Test Group");
});

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){
            if(data.d.results[0] != undefined){
                // Exist in group
            }
            else
            {
                // Does not exist
            }
        }
    });
}
</script>

Note:

If Who can view the membership of the group? in group settings is set to Everyone then CanCurrentUserViewMembership will always return true.

5
  • yes, i've already fixed that using async/await. now my problem is CanCurrentUserViewMembership always returns true
    – Aventus
    Apr 21, 2019 at 22:07
  • Use above code to check the membership of group. Keep everyone in who can view the membership of group. Apr 22, 2019 at 2:22
  • Does this answers your question? May 16, 2019 at 10:25
  • It did help me. +1 :) Jan 28, 2020 at 11:18
  • If "who can view the membership of the group?" in group settings is set to "Everyone" then CanCurrentUserViewMembership will always return true. So use updated code in my answer to check the current user group membership. Jan 28, 2020 at 11:43
2

Instead of looking at the group's members, check the current user's groups instead. This way you can keep the group locked down and not worry about someone 'peeking':

/_api/web/currentuser/?$expand=groups

Then iterate through the results and check if that group returns.

Your Answer

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

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