3

I have a list to which I want to set permission to 2 site groups. Among them, one is contribute level and second is read.

My code for assigning the permission to list is:

private void SetCalendarListPermission(SPWeb web)
{
    SPList calendar = ListHelper.GetListByUrl(web.Lists, ListNames.CalendarUrl);
    if (calendar != null)
    {
        calendar.BreakRoleInheritance(true);
        SPGroup groupEventMangers = web.SiteGroups.GetByName(GroupsAndPermissions.GroupNames.EventManagers);
        SPRoleAssignment rAssignment = new SPRoleAssignment((SPPrincipal)groupEventMangers);
        SPRoleDefinition rDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
        rAssignment.RoleDefinitionBindings.Add(rDefinition);
        calendar.RoleAssignments.Add(rAssignment);
        calendar.Update();
        SPGroup groupSiteVisitors = web.SiteGroups.GetByName(calendar.ParentWeb.Title + GroupsAndPermissions.GroupNames.Visitors);
        SPRoleAssignment rAssignmentVistor = new SPRoleAssignment((SPPrincipal)groupSiteVisitors);
        SPRoleDefinition rDefinitionVisitor = web.RoleDefinitions.GetByType(SPRoleType.Reader);
        rAssignment.RoleDefinitionBindings.Add(rDefinitionVisitor);
        calendar.RoleAssignments.Add(rAssignmentVistor);                
        calendar.Update();
    }
}

The first permission is set successfully but I am getting the error at line SPRoleDefinition rDefinitionVisitor = web.RoleDefinitions.GetByType(SPRoleType.Reader); which specifies that "Cannot add a role assignment with empty role definition binding collection.".

What am I missing?

1
  • I am getting null values in rAssignmentVisitor what to do for that?
    – users1100
    Commented Dec 20, 2013 at 13:31

2 Answers 2

2

When I add new role definition to role assignments collection I always create new instance of SPRoleDefinitionBindingsCollection and import it with SPRoleAssingment.ImportRoleDefinitionBindings method. It's not the only way I think, it's just my habit.

This code has always worked for me:

SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
SPRoleDefinition roleDefinition = new SPRoleDefinition();
roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleDefinitionBindingCollection roleDefinitionBindingCollection = new SPRoleDefinitionBindingCollection();
roleDefinitionBindingCollection.Add(roleDefinition);
roleAssignment.ImportRoleDefinitionBindings(roleDefinitionBindingCollection);
web.AllowUnsafeUpdates = true;
list.RoleAssignments.Add(roleAssignment);
web.AllowUnsafeUpdates = false;

Also try setting SPWeb.AllowUnsafeUpdates = true; because some operations may reset it automatically.

0

By trying some code samples I found the solution:

I just needed to change a single line of code. The line is: calendar.BreakRoleInheritance(true); The reason was if I place true then the roles were copied after the inheritance was break, I corrected using calendar.BreakRoleInheritance(false); that now no groups will be there and new groups will be applied to the list.

Hope this helps others too!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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