Create a New SharePoint Permission Level and Bind it to an Existing SharePoint Group
This example will create a new permission level, called Example_xxxxxxxxxxx. After creation, it binds the permission level to an existing SharePoint Group called Foo.
using (SPSite site = new SPSite( "https://moss/sites/PermExample" ))
{
using (SPWeb rootWeb = site.RootWeb)
{
string permissionLevelName = "Example_"+System.DateTime.Now.Ticks.ToString();
// Create a new Permission Level
SPRoleDefinition newPermissionLevel = new SPRoleDefinition();
newPermissionLevel.Name = permissionLevelName;
newPermissionLevel.Description = "Example Permission Level";
newPermissionLevel.BasePermissions =
SPBasePermissions.AddListItems |
SPBasePermissions.BrowseDirectories |
SPBasePermissions.EditListItems |
SPBasePermissions.DeleteListItems |
SPBasePermissions.AddDelPrivateWebParts;
// Add the permission level to web
rootWeb.RoleDefinitions.Add(newPermissionLevel);
// Bind to the permission level we just added
newPermissionLevel = rootWeb.RoleDefinitions[permissionLevelName];
// Create a new role Assignment using the SharePoint Group "Foo"
SPRoleAssignment roleAssignment = new SPRoleAssignment( (SPPrincipal)rootWeb.SiteGroups[ "Foo" ] );
// Add the Permission Level to the Foo SharePoint Group
roleAssignment.RoleDefinitionBindings.Add(newPermissionLevel);
// Add the new Role Assignment to the web
rootWeb.RoleAssignments.Add(roleAssignment);
rootWeb.Close();
}
site.Close();
}
Comments
Anonymous
October 05, 2007
PingBack from http://www.artofbam.com/wordpress/?p=5440Anonymous
August 14, 2010
I am new to SharePoint. Where to write this code?Do i need to create feature?Anonymous
August 30, 2011
Thanks for the very good snippet code.Anonymous
October 07, 2011
yoursandmyideas.wordpress.com/.../setting-custom-permission-levels-in-sharepoint-programmatically