SPRoleDefinitionCollection.Add method

Adds a new role definition to the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Sub Add ( _
    role As SPRoleDefinition _
)
'Usage
Dim instance As SPRoleDefinitionCollection
Dim role As SPRoleDefinition

instance.Add(role)
public void Add(
    SPRoleDefinition role
)

Parameters

Exceptions

Exception Condition
ArgumentException

The role definition of the current website does not inherit from the parent website.

-or-

The current website does not have the unique role definition (the HasUniqueRoleDefinitions property of the current website is false).

Examples

The following code example creates a role definition, sets property values, and adds the new role definition to the collection of role definitions for a Web site.

using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Subsite_Name"])
{
    SPRoleDefinitionCollection collRoleDefinitions = oWebsite.RoleDefinitions;
    SPRoleDefinition oRoleDefinition = new SPRoleDefinition();
    oRoleDefinition.BasePermissions = SPBasePermissions.AddListItems |
    SPBasePermissions.BrowseDirectories | SPBasePermissions.EditListItems;
    oRoleDefinition.Description = "My custom definition.";
    oRoleDefinition.Name = "Custom Role Definition";
    collRoleDefinitions.Add(oRoleDefinition);
}
Using oWebsite As SPWeb = SPContext.Current.Site.AllWebs("Subsite_Name")
   Dim collRoleDefinitions As SPRoleDefinitionCollection = oWebsite.RoleDefinitions
   Dim oRoleDefinition As New SPRoleDefinition()
   oRoleDefinition.BasePermissions = SPBasePermissions.AddListItems Or SPBasePermissions.BrowseDirectories Or SPBasePermissions.EditListItems
   oRoleDefinition.Description = "My custom definition."
   oRoleDefinition.Name = "Custom Role Definition"
   collRoleDefinitions.Add(oRoleDefinition)
End Using

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also

Reference

SPRoleDefinitionCollection class

SPRoleDefinitionCollection members

Microsoft.SharePoint namespace