2

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)

  • 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. – love thakker Apr 16 at 3:18
  • yes i've set this up already, i just need to have the code for async / await thanks – Aventus Apr 16 at 3:38
  • it seems it always returns true – Aventus Apr 17 at 2:37
0

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>
  • yes, i've already fixed that using async/await. now my problem is CanCurrentUserViewMembership always returns true – Aventus Apr 21 at 22:07
  • Use above code to check the membership of group. Keep everyone in who can view the membership of group. – Ganesh Sanap Apr 22 at 2:22
  • Does this answers your question? – Ganesh Sanap May 16 at 10:25

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.