Freigeben über


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