To check if a user is part of a group programmatically, we can use this end point:
When you execute this code, and (if) you get the below error
Navigate to the group, go to Group settings, and ensure "Who can view the membership of the group?" to "Everyone".
By default, only the Group Owner can view the membership of a Group. That means, the above code works if the logged in user executing the code is the group owner. For everyone else, it throws error. If it is fine to keep this membership view open to everyone, you have to change that setting.
$.ajax({
type: "GET",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('groupName')/users?$filter=Id eq " + userId,
headers: {"accept": "application/json; odata=verbose"},
success: function(data){
// Success logic
},
error: function(error){
alert("Error");
console.log(error);
}
});
When you execute this code, and (if) you get the below error
Access denied. You do not have permission to perform this action or access this resource
401
System.UnauthorizedAccessException
Navigate to the group, go to Group settings, and ensure "Who can view the membership of the group?" to "Everyone".
By default, only the Group Owner can view the membership of a Group. That means, the above code works if the logged in user executing the code is the group owner. For everyone else, it throws error. If it is fine to keep this membership view open to everyone, you have to change that setting.
Comments
Post a Comment