1
votes

I have to add users into SharePoint online SubSite's Members and Owner groups using CSOM. I am using Sub site's Context for this. Below method is used to get group name, i am passing Owners/Members as group name and then adding it to the group returned by below method, but it is always adding to site collection level groups:

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
1

1 Answers

0
votes

By default, sharePoint sub sites use the same permissions as parent site. So the sub sites use the same groups as site collection level groups. Adding users to the subsite group equals adding them to the site collection level groups.

You could delete the uqiue permissions in the subsite and create new group for the subsite.

enter image description here

enter image description here