RoleAssignmentCollection.Add Method
Adds a role assignment to the collection of role assignment objects.
Namespace: Microsoft.SharePoint.Client
Assemblies: Microsoft.SharePoint.Client.Silverlight (in Microsoft.SharePoint.Client.Silverlight.dll); Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)
Syntax
'Declaration
<RemoteAttribute> _
Public Function Add ( _
principal As Principal, _
roleBindings As RoleDefinitionBindingCollection _
) As RoleAssignment
'Usage
Dim instance As RoleAssignmentCollection
Dim principal As Principal
Dim roleBindings As RoleDefinitionBindingCollection
Dim returnValue As RoleAssignment
returnValue = instance.Add(principal, _
roleBindings)
[RemoteAttribute]
public RoleAssignment Add(
Principal principal,
RoleDefinitionBindingCollection roleBindings
)
Parameters
principal
Type: Microsoft.SharePoint.Client.PrincipalA Principal object that represents a user or group that can be assigned permissions in Microsoft SharePoint Foundation to control security.
roleBindings
Type: Microsoft.SharePoint.Client.RoleDefinitionBindingCollectionA RoleDefinitionBindingCollection object that defines the role definitions that are bound to the role assignment object.
Return Value
Type: Microsoft.SharePoint.Client.RoleAssignment
A RoleAssignment object that represents the role assignment to add.
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | The parent securable object does not have unique permissions. |
UnauthorizedAccessException | The current user does not have sufficient permissions to manage permissions on the parent securable object. |
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 RoleAssignmentCollection_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
RoleAssignmentCollection Class