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?