Home » SharePoint 2010 » SharePoint 2010 – Adding Group to Sub site Programmatically

SharePoint 2010 – Adding Group to Sub site Programmatically


Before we start discussing on how to add groups, do role assignment, add users to group lets discuss about  SPWeb.Groups and SPWeb.SiteGroups

SPWeb.SiteGroups

This is Group Collection object for site collection. When you want to add a group to site collection or sub Site, group can be added to this collection.

SPWeb.Groups

All sub sites that are inheriting permissions from a parent site will see the groups added using SPWeb.SiteGroups.Add() when iterating through the SPWeb.Groups collection

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(NewWorkshopURL))
           {
               using (SPWeb Web = Site.OpenWeb())
               {
                   using (SPWeb Sitecollection = Site.OpenWeb())
                   {
                       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;
               }
           }

and here you go!!

About these ads

6 Comments

  1. Kanishka says:

    Thanx mate..Was Useful to me

  2. shubhasis says:

    Hi I want to get the site url’s (including subsite) which a SP User (having some group permission may or may not only in top levele site but also in some sub sites) can browse in the website.

    or rather is there any way to get the browseable site urls of a SPUser having some group permission both on top level site or only in subsites.

    i was trying with the below code ——-

    public ArrayList GetUserPermissionOnSiteUrls(ArrayList siteGUIDs, string userName)
    {
    SPTlogger.Debug(“–>: GetUserPermissionOnSiteUrls() UserName :” + userName);
    ArrayList spActiveUrlList = new ArrayList();
    foreach (string siteGUID in siteGUIDs)
    {
    try
    {

    SPSite siteCollection = new SPSite(new Guid(siteGUID));
    SPWeb spWeb = siteCollection.OpenWeb();
    SPUser spUser = null;
    try
    {
    spWeb.AllowUnsafeUpdates = true;
    spUser = spWeb.EnsureUser(userName);
    spWeb.AllowUnsafeUpdates = false;

    }
    catch (Exception ex) { }
    finally
    {
    spWeb.AllowUnsafeUpdates = false;
    }
    if (spUser == null)
    {
    spUser = spWeb.SiteUsers[userName];

    }

    SPWebCollection collWebsite = siteCollection.AllWebs;
    for (int i = 0; i < collWebsite.Count; i++)
    {
    foreach (SPGroup group in spUser.Groups)
    {
    foreach (SPGroup grpname in collWebsite[i].Groups)
    {
    try
    {
    if (group.Name == grpname.Name)
    {
    spActiveUrlList.Add( collWebsite[i].Url);
    }
    }
    catch (Exception e)
    {
    SPTlogger.Info("User is not present in this group " + group.Name);
    }
    }
    }
    }

    }

    catch (Exception e)
    {

    }

    SPTlogger.Debug("<–: GetUserPermissionOnSiteUrls()");

    }
    return spActiveUrlList;
    }

    pls help..

  3. Marcus says:

    Thanks for the code, but why do you call
    Web.BreakRoleInheritance(true);

    and then you remove all role assignments later on?
    Web.BreakRoleInheritance(false);

    WOuld do the same, or?

    • pathikhrawal says:

      Hi Marcus,

      This is optional Web.BreakRoleInheritance(true); this will break the inheritance from parent site.

      My post code will set unique permission to web.

      If you do not want to set unique permission, you can remove the code of breakinheritance

    • pathikhrawal says:

      Hi Marcus,

      The answer of your question is

      1) Web.Breakinheritance(true)– will remove inheritance to this web, as I do not want to have same permission as given to parent. I want to set unique permission to this web.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 81 other followers

%d bloggers like this: