Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I know how to grant someone certain role to a list (contribute or read for example) using C# such as:

                    SPRoleAssignment userRoleAssignmentCont = new SPRoleAssignment(allUsers);
                    SPRoleDefinition userRoleDefinitionCont = web.RoleDefinitions["Contribute"];
                    userRoleAssignmentCont.RoleDefinitionBindings.Add(userRoleDefinitionCont);
                    list.RoleAssignments.Add(userRoleAssignmentCont);

But how do I do the exact same thing but instead of the list I grant permission to a group of people/permissions list. So basically - the user/users in question will be granted contribute permissions to add, remove or update people to a certain group (special administrators for example).

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Hope this helps!

public void addPermissionToGroup()
{
    SPSite site = new SPSite("http://mysite:5050/");
    SPWeb spWeb = site.OpenWeb();
    string permissionName = "Read";
    string groupName = "Project Manager";

 try
 {
    spWeb.AllowUnsafeUpdates = true;
    SPRoleAssignment roleAssignment = new SPRoleAssignment(spWeb.SiteGroups[groupName]);
    roleAssignment.RoleDefinitionBindings.Add(spWeb.RoleDefinitions[permissionName]);

    if (!spWeb.HasUniqueRoleAssignments)
        spWeb.BreakRoleInheritance(false);

    spWeb.RoleAssignments.Add(roleAssignment);
    spWeb.Update();
}
catch (Exception _exception)
{
    throw _exception;
}
finally
{
    spWeb.AllowUnsafeUpdates = false;
}
}

For adding user to Group:

public void AddUsers(string groupname, string username)
{
 try
 {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
           // Gets a new security context using SHAREPOINT\system
           using (SPSite site = new SPSite(SPContext.Current.Site.Url))
           {
                 using (SPWeb thisWeb = site.OpenWeb())
                 {
                      thisWeb.AllowUnsafeUpdates = true;
                      SPUser Name = thisWeb.EnsureUser(username);
                      thisWeb.Groups[groupname].AddUser(Name);
                      thisWeb.AllowUnsafeUpdates = false;
                 }
            }
  });

 }

 catch (Exception ex)
 {
   //Log error here.
  }
 }

Set GroupA as owner of GroupB

SPSecurity.RunWithElevatedPrivileges(delegate
{
  using (SPSite site = new SPSite("http://site02"))
  using (SPWeb web = site.OpenWeb())
  {
    web.AllowUnsafeUpdates = true;
   SPGroup visitors = web.SiteGroups["GroupB"];
   SPGroup members = web.SiteGroups["GroupA"];
   visitors.Owner = (SPMember)members;
   visitors.Update();
   web.AllowUnsafeUpdates = false;
 }
});
share|improve this answer
    
Sorry, my explanation was possibly wrong. I want to give a user permission to manage user groups (add users to a particular group etc.) –  DeeMac Nov 5 '12 at 10:06
    
Please look the edited answer here you have to pass username, and group. –  Anuja Nov 5 '12 at 10:14
    
another solution could be by "/UserGroup.asmx" also. –  Anuja Nov 5 '12 at 10:15
    
This still hasn't really addressed my initial problem. Thanks a lot for your help so far but what I'm looking to do is give a user group permission to manage a user group. So if I belong to GroupA - I am permitted to add/remove/update users belonging to GroupB. –  DeeMac Nov 5 '12 at 10:19
1  
ok, I got your question, for that you have to set the GroupB owner property with the name of GroupA. Editing my answser with code. –  Anuja Nov 5 '12 at 10:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.