Search code examples
c#sharepointsharepoint-2010sharepoint-2007sharepoint-api

Adding existing security group to a subsite


I created several groups using the following code:

                SPWeb currentSite = SPContext.Current.Web;
                currentSite.EnsureUser(groupOwner);
                currentSite.SiteGroups.Add(siteTitle1, currentSite.SiteUsers[groupOwner],
                currentSite.SiteUsers[groupOwner], siteDescription1);

How can I assign these groups to a specific subsite?


Solution

  • You should get a subsite object, like this:

    var subWeb = currentSite.Webs["MySubSite"];
    

    And then use your code:

    subWeb.EnsureUser(groupOwner);
    subWeb.SiteGroups.Add(siteTitle1, currentSite.SiteUsers[groupOwner],
                subWeb.SiteUsers[groupOwner], siteDescription1);