3

Whenever I create a new user group in a subsite, the same user group is getting added at the top level site as well. Is it possible to create a new User Group on the subsite level only, either programmatically or not?

2

No. Permission groups live in Site Collections (Sites), not sites (Webs).

1

If you try to add a group using SPWeb.Groups.Add(), you will get below error

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

Solution To resolve above error follow below steps

1) Add group to site collection level by using

Sitecollection.SiteGroups.Add(GroupName, Web.SiteAdministrators[0], null, "Default Group for WorkShop Site");

2) Now again get the group

SPGroup secGroup = Sitecollection.SiteGroups[GroupName];

3) Populate a Role Assignment as shown below

SPRoleAssignment roleAssignment = new SPRoleAssignment(secGroup);

4) Define a Role Definition

SPRoleDefinition roleDefinition = Sitecollection.RoleDefinitions["Contribute"];

5) Add role Definition to

roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

6) Break the Inheritance

Web.BreakRoleInheritance(true);

7) Get the list of Role Assignments to list item and remove one by one

SPRoleAssignmentCollection SPRoleAssColn = Web.RoleAssignments;
                      for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
                      {
                          SPRoleAssColn.Remove(i);
                      }

8)finally add the group

   Web.RoleAssignments.Add(roleAssignment);

Complete Code:

using (SPSite Site = new SPSite("Your site url"))
           {
               using (SPWeb Web = Site.OpenWeb())
               {
                   using (SPWeb Sitecollection = Site.OpenWeb())
                   {
                       var GroupName = "Your group name";
                       Sitecollection.SiteGroups.Add(GroupName, Web.SiteAdministrators[0], null, " Default Group for WorkShop Site");
                       SPGroup secGroup = Sitecollection.SiteGroups[GroupName];
                       SPRoleAssignment roleAssignment = new SPRoleAssignment(secGroup);
                       SPRoleDefinition roleDefinition = Sitecollection.RoleDefinitions["Contribute"];
                       roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                       Web.BreakRoleInheritance(true);
                       //Get the list of Role Assignments to list item and remove one by one.
                       SPRoleAssignmentCollection SPRoleAssColn = Web.RoleAssignments;
                       for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
                       {
                           SPRoleAssColn.Remove(i);
                       }
                       Web.RoleAssignments.Add(roleAssignment);
                   }
                   returnCondition = true;
               }
           }

Link to source

0

yes it is possible we can create new groups at sub site level only,

Following is the link where they have created sites as well as groups programatically,

http://sharepointlive.blogspot.in/2009/06/how-to-create-create-new-sharepoint.html

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.