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.

其他提示

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);
            web.AssociatedGroups.Add(web.SiteGroups[groupName]);
        }

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

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

        web.Update();
    }
许可以下: CC-BY-SA归因
scroll top