Skip to main content

Posts

Showing posts from July, 2018

UnauthorizedAccessException while checking If user is part of a Group

To check if a user is part of a group programmatically, we can use this end point:

$.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…