我必须使用CSOM将用户添加到SharePoint Online SubSite的“成员”和“所有者”组中。 我为此使用Sub site的Context。下面的方法用于获取组名,我将所有者/成员作为组名传递,然后将其添加到下面的方法返回的组中,但它始终添加到网站集级别组:
private async Task<Group> GetGroupOrDefaultAsync(ClientContext context, Constants.SPDefaultGroup spDefaultGroup)
{
Group group = null;
string groupName = string.Empty;
switch (spDefaultGroup)
{
case Constants.SPDefaultGroup.Members:
groupName = Common.Configuration.Constants.MatterCustomMemberGroup;
if (string.IsNullOrWhiteSpace(groupName))
{
group = context.Web.AssociatedMemberGroup;
}
break;
case Constants.SPDefaultGroup.Owners:
groupName = Common.Configuration.Constants.MatterCustomOwnerGroup;
if (string.IsNullOrWhiteSpace(groupName))
{
group = context.Web.AssociatedOwnerGroup;
}
break;
case Constants.SPDefaultGroup.Visitors:
groupName = Common.Configuration.Constants.MatterCustomVisitorGroup;
if (string.IsNullOrWhiteSpace(groupName))
{
group = context.Web.AssociatedVisitorGroup;
}
break;
}
if (group == null)
{
if (!string.IsNullOrWhiteSpace(groupName))
{
// get group by name configured in the web.config
group = await GetGroupByNameAsync(context, groupName);
}
else
{
_logger.Error(new Exception($"Sharepoint Group not found. Searching group for {spDefaultGroup.ToString()}"), "SharePointService.AssignPeopleToGroup error.");
}
}
return group;
}
Code to add user:
var user = context.Web.EnsureUser(name);
group.Users.AddUser(user);
await context.ExecuteQueryRetryAsync();
enter code here