I'm trying to grant a user permission to a SharePoint site using PowerShell and the client object model. The code I'm using is as follows:
# Setup context
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web=$context.Web
# New user object
$user=$web.EnsureUser("domain\username");
# Role definition
$rd = $web.RoleDefinitions.GetByName("Full Control")
# Create role definition binding collection
$rdbc=New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
$context.ExecuteQuery();
$rdbc.Add($rd)
$context.Web.RoleAssignments.Add($user, $rdbc)
$context.ExecuteQuery();
This fails on the $context.Web.RoleAssignments.Add($user, $rdbc)
line with the following exception:
The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. At :line:0 char:0
I've tried placing additional ExecuteQuery
statements around the RoleAssignments.Add
call but the result is the same.
Any ideas?