这是我的决定
var clientContextForGroup = new SP.ClientContext.get_current();
var siteGroups = clientContextForGroup.get_web().get_siteGroups();
var web = clientContextForGroup.get_web();
spGroup = siteGroups.getByName(GroupName);
var userCollection = spGroup.get_users();
$.each(MembersArray, function (index, tempitem) {
var userCreationInfo = new SP.UserCreationInformation();
userCreationInfo.set_email(tempitem.EMail);
userCreationInfo.set_loginName(tempitem.Name);
userCreationInfo.set_title(tempitem.Title);
userCollection.add(userCreationInfo);
});
clientContextForGroup.load(userCollection);
clientContextForGroup.executeQueryAsync(function () {
console.log("added in SPGroup");
}, function (sender, args) {
console.log("Sorry Some Error occurred in adding user in SPGroup...");
});
如果'MembersArray'中有成员,则添加新成员。 我想用新成员替换所有成员。
答案 0 :(得分:0)
如果MemberArray中不存在用户,则可以调用removeByLoginName 删除用户。
来自here的示例代码。
var clientContext = new SP.ClientContext(siteUrl);
var oWeb = clientContext.get_web()
var collGroup = oWeb.get_siteGroups();
var oGroup = collGroup.getById(7); //Change 7 based on the group's id
var oUser = oGroup.get_users();
clientContext.load(oUser);
var user = web.ensureUser("domain\username);
oUser.removeByLoginName(user.get_loginName());
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded(){
alert('User Removed');
}
function onQueryFailed() {
alert('Failed');
}