SPRoleDefinitionCollection.Add Method
Adds a new role definition to the collection.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
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
role
Type: Microsoft.SharePoint.SPRoleDefinitionThe role definition to be added.
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