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..
Thanks
You need to add a role definition binding to the role assignment, for example to add a contributor role definition
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));
This should work.
list.BreakRoleInheritance(true);
SPGroup groupAdmin = web.SiteGroups["IKM Manager"];
SPRoleAssignment roleAssignmentAdmin = new SPRoleAssignment((SPPrincipal)groupAdmin);
SPRoleDefinition roleAdmin = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignmentAdmin.RoleDefinitionBindings.Add(roleAdmin);
list.RoleAssignments.Add(roleAssignmentAdmin);
list.Update();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With