Reputation: 339
I'm trying to iterate over a dataset of SharePoint groups Id's, via the REST API, and process the results. The thing is, I don't have access to all the groups (this is expected). In this case, I'd like to log the group ID as "access denied" and move on to the next one.
When the code gets to the first group that I don't have access to, I'm prompted for credentials, as anticipated. Whether I enter cred's or just click "cancel" I I get the anticipated 401 error and the script just exits with-
HTTP401: DENIED - The requested resource requires user authentication.
(XHR)GET - https://{sourceSharePointSite}/_api/Web/SiteGroups/GetById({groupID})/Users
So, yes, I get the ID of the first group I don't have access to, but the script just exits and I really need to check the rest of the groups (more than 400 of them. don't ask)
Here's my code...
function getMultiGroupMembers(arrGroups){// arrGroups is an object defined earlier
$(arrGroups).each(function(i){
var myId = arrGroups.results[i].Id;
var myTitle = arrGroups.results[i].Title;
log("Group ID: "+myId+" Group Name: "+myTitle+ " Members:");
getData(myId).then(function() {
log( "getMultiGroupMembers success" );
}, function() {
log( "getMultiGroupMembers fail" );
})
});
}
function getData(groupId) {
var url = myUrl + "/_api/Web/SiteGroups/GetById("+groupId+")/Users";
return $.getJSON(url).then(function(data){
log("getGroupMembers() success: " + data);
arrUsers = jsonToCsv(data.d); // convert the JSON object to CSV for exporting
log(JSON.stringify(arrUsers)); // display the results in the log
}, function(err) {
var dfd = $.Deferred();
dfd.reject(err)
return dfd.promise();
})
}
Really not sure what I'm missing. I've tried in MS Edge and Chrome
Upvotes: 0
Views: 367
Reputation:
Use this endpoint: _api/web/currentuser/groups . It will show all groups that user has access. Use /_api/Web/SiteGroups to get all groups. Filter second by first and you will get you want.
May be you can change logic to another?
1. Get all groups that you have access. Just call "/_api/Web/SiteGroups" without any additional parameters. SharePoint REST API must return you only available to you groups. Exclude all gotten groups from your predefined list. All remaining groups from predefined list would be that you want to found, i.e. with "access denied".
I understand that this logic not consider not existed groups. But may be your current logic also not consider not existed groups. If you don't have access to group then you cannot understand if it is existed or not.
2. Also if you use SharePoint On-premise then you can create web service. Web service will get all groups by elevated privelege under some service account. =) And you can call this web service methods.
Upvotes: 1