Share via


RoleDefinitionBindingCollection.Add method

Adds the specified role definition to the binding collection.

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

Syntax

'Declaration
Public Sub Add ( _
    roleDefinition As RoleDefinition _
)
'Usage
Dim instance As RoleDefinitionBindingCollection
Dim roleDefinition As RoleDefinition

instance.Add(roleDefinition)
public void Add(
    RoleDefinition roleDefinition
)

Parameters

Exceptions

Exception Condition
SPException

Cannot grant a guest role or role definition is already added. Error code: -2146232832.

Remarks

The parent Web of the role definition being added should be the same as the parent Web of existing role definitions in the collection. It must not be a null reference (Nothing in Visual Basic).

Examples

This code example creates a new permission level and adds a user to the Announcements list with that permission level.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class RoleDefinitionBindingCollection_AddExample
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Site collSite = clientContext.Site;
            Web site = clientContext.Web;

            // Set up permissions.
            BasePermissions permissions = new BasePermissions();
            permissions.Set(PermissionKind.ViewListItems);
            permissions.Set(PermissionKind.AddListItems);
            permissions.Set(PermissionKind.EditListItems);
            permissions.Set(PermissionKind.DeleteListItems);

            // Create a new role definition.
            RoleDefinitionCreationInformation rdcInfo = new RoleDefinitionCreationInformation();
            rdcInfo.Name = "Manage List Items";
            rdcInfo.Description = "Allows a user to manage list items";
            rdcInfo.BasePermissions = permissions;
            RoleDefinition roleDef = collSite.RootWeb.RoleDefinitions.Add(rdcInfo);

            // Create a new RoleDefinitionBindingCollection object.
            RoleDefinitionBindingCollection collRDB = new RoleDefinitionBindingCollection(clientContext);
            // Add the role to the collection.
            collRDB.Add(roleDef);

            // Get the list to work with and break permissions so its permissions can be managed directly.
            List targetList = site.Lists.GetByTitle("Announcements");
            targetList.BreakRoleInheritance(true, false);

            // Get the RoleAssignmentCollection for the target list.
            RoleAssignmentCollection collRoleAssign = targetList.RoleAssignments;
            // Add the user to the target list and assign the user to the new RoleDefinitionBindingCollection.
            RoleAssignment rollAssign = collRoleAssign.Add(site.CurrentUser, collRDB);

            clientContext.ExecuteQuery();

            Console.WriteLine("Security modified");
        }
    }
}

See also

Reference

RoleDefinitionBindingCollection class

RoleDefinitionBindingCollection members

Microsoft.SharePoint.Client namespace