Code to create a SharePoint list with predefined permission
Posted by ken zheng on October 19, 2009
This is just a sample code for me to create a list with a predefined permission
private void SetPermissionsForTheList(SPList selectedList)
{
// breaks the inheritance from the parent
selectedList.BreakRoleInheritance(true);
//removes all the permissions from the list, except for 'site - admins'
while (selectedList.RoleAssignments.Count > 0)
selectedList.RoleAssignments.Remove(0);
if (selectedList.RoleAssignments.Count == 0)
{
// get the defined site admins group
SPGroupCollection groupCollection = selectedList.ParentWeb.SiteGroups;
SPGroup group = groupCollection["Site - Admins"];
// get the full access role definition
SPRoleDefinitionCollection roleDefCollection = selectedList.ParentWeb.RoleDefinitions;
SPRoleDefinition roleDefinition = roleDefCollection [0];
// create a role assignment based on the site admin group
// and bind to the full access role definition
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
selectedList.RoleAssignments.Add(roleAssignment);
}
}