Credentials popup issue while adding a user to SharePoint group with REST API
题
I am using SharePoint 2013 On-premise,
We have two SharePoint groups(Group-A, Group-B) in a SharePoint site. Group-A is owner group of Group-B, Generally Group-A users can add,delete users from Group-B.
when Group-A user trying to add a user to Group-B with following REST API code in button click, immediately a credentials popup is coming. when i checked the group B, user was added successfully.
Below is my code on button click. I am using Content editor web part.
function (webAbsoluteUrl, groupId, userLoginName, success, failure) {
var requestUri = webAbsoluteUrl + "/_api/web/sitegroups(" + groupId + ")/users"
var userDataItem = {
__metadata: {
type: 'SP.User'
},
LoginName: userLoginName
};
$.ajax({
url: requestUri,
type: "POST",
async: false,
data: JSON.stringify(userDataItem),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
success(data);
},
error: function (data) {
failure(data);
}
});
}
Here my issue is::
- If Site-collection administrator login into the page, can able to add users to Group-B without any issues(no pop Up).
- Group-A user login into the page, can able add himself to Group-B without any issues(no pop Up).
- But Group-A user login into the page and trying to add other users to Group-B , User was added but immediately showing "SP Credentials pop-up".
I got following error in console::
"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"
解决方案
For adding user to group, instead of REST API I used JSOM, now I didn't get credentials pop up. :)
Adding users to a group with JSOM using following code::
addUserToSharePointGroup: function (loginName, success, failure) {
var _this = this;
var clientContext = new SP.ClientContext.get_current();
var siteGroups = clientContext.get_web().get_siteGroups();
var web = clientContext.get_web();
spGroup = siteGroups.getByName(_this.group_AuditProjectMangers);
user = web.ensureUser(loginName);
var userCollection = spGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(spGroup);
clientContext.executeQueryAsync(success, failure);
}
Hope this one might help to others... :)
Ornery Walrus thanks for response....