I am using SharePoint 2013 online.
We have SharePoint groups and there the group owner can successfully add or remove the users. But when accessing the user groups through JSOM in my angular app which deployed on SharePoint I get
ErrorCode: -2147024891 ErrorMessage: "Access denied. You do not have permission to perform this action or access this resource." ErrorTypeName: "System.UnauthorizedAccessException"
Here's the code I used to add users to SharePoint user group:
addUserJSOM() {
const loginName = `i:0#.f|membership|${this.form.value.email}`;
var clientContext = new SP.ClientContext.get_current();
var siteGroups = clientContext.get_web().get_siteGroups();
var web = clientContext.get_web();
let spGroup = siteGroups.getByName('Team WhatIf');
let user = web.ensureUser(loginName);
var userCollection = spGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(spGroup);
clientContext.executeQueryAsync(this.onQuerySucceeded, this.onQueryFailed);
}
but the site administrator and access to adding or deleting user from user groups via JSOM.