Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
using (SPSite site = (SPSite)properties.Feature.Parent)
{
   using (SPWeb web = site.OpenWeb())
   {
        if (web != null)
        {
          web.AllowUnsafeUpdates = true;
          SPList list = web.Lists["Alert Status v1.0"];

          //Creates a new role assignment for a group
          SPGroup myGroup = web.SiteGroups["IKM Manager"];
          SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;

          // SPRoleAssignment accepts a SPPrincipal which can be a SPUser or SPGroup
          SPRoleAssignment roleAssignment = new SPRoleAssignment(myGroup);

          //Add the new role assignment to the collection of role assignments for the site.
          roleAssignments.Add(roleAssignment);

          // Stop inheriting permissions
          list.BreakRoleInheritance(true);
          list.RoleAssignments.Add(roleAssignment);
          list.Update();

I have error on RoleAssignments.Add(roleAssignment) like "Cannot add a role assignment with empty role definition binding collection". I want to stop inheriting permissions and add specific group to List permissions. Can you help me? please..

share|improve this question
    
Welcome to the site. –  RJ Cuthbertson Aug 1 '12 at 20:53

2 Answers 2

up vote 1 down vote accepted

You need to define the role (as in what level of permission to grant the group).

The class is SPRoleDefinition.

SPRoleAssignment roleAssignment = new SPRoleAssignment(myGroup);
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
roleAssignments.Add(roleAssignment);

See: http://stackoverflow.com/questions/358647/programatically-add-user-permission-to-a-list-in-sharepoint

share|improve this answer
    
thank you for your answer but normally I have IKM Manager group which is in root site permission, has Full Control. My list is inheriting permissions. I want to stop inheritance and set the just IKM Manager group to my list. So, I need to know that do I have to set roledefinition to this group? because it is defined already!! –  mkacar Aug 1 '12 at 20:55
    
I updated my post to reflect this. It's two lines of code between your two lines. SPRoleType.Administrator is Full Control in this respect. –  RJ Cuthbertson Aug 2 '12 at 12:42

1) You need to use list.BreakRoleInheritance(false). Using true copies the existing role assignments to the list - so everyone who had access to the site would continue to have access to the list. Using false clears any role assignments - so nobody has any access, until you add your new one.

2) As mentioned above, you have to use a Role Definition to assign how much access a particular Role Assignment provides.

roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));

The SPRoleType enumeration provides a shortcut to the predefined permissions levels. You can also set rights individually for a Role Definition using it's BasePermissions.

share|improve this answer

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.