0

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?

MVZ
  • 2,220
  • 5
  • 27
  • 57
  • When you add a group at site collection level, group gets automatically added to subsite and vice versa. Means there is only one group which is shown at all the levels but permissions are different at different levels.So, a group can't be created only at a subsite level. Can you please tell the exact requirement – user270014 Feb 28 '12 at 04:50
  • How would I create a group for a subsite only?? – MVZ Feb 29 '12 at 16:50

1 Answers1

1

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);
Aviw
  • 1,056
  • 11
  • 14
  • Thank you! Under "MySubSite" Do I need to have the full path of the subsite? Something like "firstsite/secondsite/subsite"? – MVZ Feb 28 '12 at 23:13
  • If you want to get a specific site, you should use: using(var site = new SPSite("http://mysite/mysubsite/")) { using(var web = site.OpenWeb()) { //your code } } My previous code gets subsite of current site only. – Aviw Feb 29 '12 at 21:25