I have the following inside my c# console application

 using (SPSite site = new SPSite(scurl))  //site collection url
       using (SPWeb web = site.OpenWeb(subsiteurl0)) //specific subsite url
       {//code goes here
            subSite.Groups.Add(currentgroupname, site.RootWeb.SiteAdministrators[0], site.RootWeb.SiteAdministrators[0], "desc");

Now, I was surprised that the SPWeb.Groups property has a method named Add().. so I tried to test it using the above code but i got this exception :-

You cannot add a group directly to the Groups collection.  You can add a group to the SiteGroups collection.

Now, I know that I can fix this by replacing subsite.Groups.Add() with subsite.siteGroups.Add() and it will work well.

But my question is why does SharePoint provide SPWeb.Groups.Add() method that we can not use? or there are scenarios that we can add a group from the SPWb.Groups. Now, I tried this on a SPWeb that have unique permission and on another which inherit its permissions but both raise the same exception as described above.


The short answer to this question is, Add method is there for Type consistency which makes the life easier for the Developers, both the SharePoint library(dll) developers and general SharePoint Developers(dll integrators).

Since the Type of SPWeb.Groups property is Microsoft.SharePoint.SPGroupCollection, it has to have the Add method.

Now, one could argue that another Type could have been created for SPWeb.Groups property but that would have:

First> added a lot of repeated code to the SharePoint library codebase and decreased the potential maintainability,

Second> enlarged the learning curve for general SharePoint Developers as now they need to remember two different Types and their members.

| improve this answer | |

Let me first elaborate the difference between SPWeb.Groups and SPWeb.SiteGroups

  • SPWeb.Groups Get a collection that contains all the groups for the subsite.

  • SPWeb.SiteGroups Get a collection that contains all the groups in the site collection.

Regarding the error:

  • You got this error because group permissions are located in site collections, not subsites.

Regarding why Microsoft added Add() function to Groups?

The Groups in this syntax is considered as a GroupCollection that already has Add() function to add a group to the collection of groups in a site collection not a subsite. Meanwhile, you can't use it with this way Groups.Add() where you will get your above error. and to overcome this issue you should use AssociatedGroups.Add as mentioned in the next point.

Regarding How to add a Group to Subsite Programmatically

You should first add the group to site collection level as you have mentioned, then use AssociatedGroups.Add ,

Try the following code.

private static void AddGroup(SPWeb web, SPRoleType roleType, string groupName)
        var groups = web.SiteGroups;

        var userGroup = FindSiteGroup(web.Site, groupName);

        if (userGroup == null)
            groups.Add(groupName, web.CurrentUser, null, string.Empty);

        var grp = web.SiteGroups[groupName];
        grp.OnlyAllowMembersViewMembership = false;

        if (roleType != SPRoleType.None)
            var asgn = new SPRoleAssignment(web.SiteGroups[groupName]);
            var roleDef = web.RoleDefinitions.GetByType(roleType);

| improve this answer | |
  • 1
    M.Qassas thanks for the repy,, but correct me if i am wrong.. now i do not need to explicitly associate a group to a SPWeb as follow web.AssociatedGroups.Add(web.SiteGroups[groupName]); ,, because when you define web.RoleAssignments.Add(asgn); this will automatically associate the group to the web ..is this correct? – john Gu Jan 16 '17 at 23:36
  • Yes @johnG it's correct , AssociatedGroups will make your group added to sub-site in the People and Group page. and once you add role assignment this group will take a permission to your site. – Mohamed El-Qassas MVP Jan 17 '17 at 8:02
  • i do not think this is 100% correct... as when you create a new Group on the rootWeb and you specify not to have any permission, then this new group will be shown inside the "people and Group" for all the subsites but not inside the "site permission" ... so i am not sure if web.AssociateGroup is requrired in this case also.. can u adivce please? – john Gu Jan 17 '17 at 11:38
  • To make it clear. let's do the following scenario, open your subsite in the browser, go to site settings people and group, add a new group, this group will be added directly the root site collection in people and group page and all subsite people and group page! Meanwhile, it will be added also to the site permission for only this subsite, and for this, you need to use the web.AssociateGroup.Add() to do the same behavior, – Mohamed El-Qassas MVP Jan 17 '17 at 12:33
  • 1
    i mean .. first you create a RoleAssignment() and you pass a Group. then you create a RoleDefinition and assign it permission level (Read, contribute), then you bind the RoleDefinition to the RoleAssignment,, finally you associate the RoleAssignment to the related SPWeb(). so seems these steps will cover Associating a Group to the SPWeb()... so no need to explicitly do web.AssociatedGroups.Add(web.SiteGroups[groupName]); is this correct? – john Gu Jan 17 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.