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)
        SPGroup groupEventMangers = web.SiteGroups.GetByName(GroupsAndPermissions.GroupNames.EventManagers);
        SPRoleAssignment rAssignment = new SPRoleAssignment((SPPrincipal)groupEventMangers);
        SPRoleDefinition rDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
        SPGroup groupSiteVisitors = web.SiteGroups.GetByName(calendar.ParentWeb.Title + GroupsAndPermissions.GroupNames.Visitors);
        SPRoleAssignment rAssignmentVistor = new SPRoleAssignment((SPPrincipal)groupSiteVisitors);
        SPRoleDefinition rDefinitionVisitor = web.RoleDefinitions.GetByType(SPRoleType.Reader);

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?

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

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();
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;

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

| improve this answer | |

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!!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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