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)