Share via


RoleAssignmentCollection class

Represents a collection of RoleAssignment objects that defines all the role assignments for each securable object.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Client.ClientObject
    Microsoft.SharePoint.Client.ClientObjectCollection
      Microsoft.SharePoint.Client.ClientObjectCollection<RoleAssignment>
        Microsoft.SharePoint.Client.RoleAssignmentCollection

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

Syntax

'Declaration
Public NotInheritable Class RoleAssignmentCollection _
    Inherits ClientObjectCollection(Of RoleAssignment)
'Usage
Dim instance As RoleAssignmentCollection
public sealed class RoleAssignmentCollection : ClientObjectCollection<RoleAssignment>

Remarks

Use the RoleAssignments property of the SPPermissionInfo, SecurableObject, List, ListItem, or Web class, to return the collection of role assignments for the given object.

To create a role assignment that has no role definition bindings, use a RoleAssignment constructor. To add bound role definitions to a role assignment, use the ImportRoleDefinitionBindings method. Use the Add(RoleAssignment) method of the RoleAssignmentCollection class to add a role assignment to the collection of role assignments for the object.

Use an indexer to return a single item from the collection. For example, if the collection is assigned to a variable named myRoleAssignments, use myRoleAssignments[index] in C# or myRoleAssignments(index) in Visual Basic, where index is either the index number of the item in the collection or a string containing the name of the role definition.

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 RoleAssignmentCollectionExample
    {
        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");
        }
    }
}

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See also

Reference

RoleAssignmentCollection members

Microsoft.SharePoint.Client namespace