0

I try to set specific permission on list items. I found some examples but I'm working in Powershell and I get a strange error

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.

Here's my code:

 foreach($item in $collListItem) { $srcContext.Load($item) $srcContext.ExecuteQuery() $item.BreakRoleInheritance($false, $false); $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($srcContext) $PermissionLevel = $srcContext.Web.RoleDefinitions.GetByName("Lecture") #userToAdd $user = $srcWeb.SiteUsers.GetByLoginName("i:0#.w|domain\test"); $srcContext.Load($user) $srcContext.ExecuteQuery() $RoleDefBind.Add($PermissionLevel) $Assignments = $item.RoleAssignments $srcContext.Load($Assignments) $srcContext.ExecuteQuery() $item.Update() $srcContext.ExecuteQuery() $Assignments.Add($user,$RoleDefBind) $srcContext.Load($Assignments) $srcContext.ExecuteQuery() } 

The error is always on the line $Assignments.Add($user,$RoleDefBind)

I added a lot of Load and ExecuteQuery... I can debug and see that Assignments exists, same for user and RoleDefBind.

I really don't understand what's missing. Please help.

Thanks

1 Answer 1

1

In my opinion, this is a known issue in PS ClientRuntimeContext.Load method. Can you try the following?

foreach($item in $collListItem) { $srcContext.Load($item) $srcContext.ExecuteQuery() $item.BreakRoleInheritance($false, $false); $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($srcContext) $PermissionLevel = $srcContext.Web.RoleDefinitions.GetByName("Lecture") #userToAdd $user = $srcWeb.SiteUsers.GetByLoginName("i:0#.w|domain\test"); $srcContext.Load($user) $srcContext.ExecuteQuery() $RoleDefBind.Add($PermissionLevel) $Assignments = $item.RoleAssignments $srcContext.Load($Assignments) $srcContext.ExecuteQuery() $srcContext.Load($Assignments.Add($user,$RoleDefBind)) $item.Update() $srcContext.ExecuteQuery() } 

You need to place the role assignments add within the context load method.

2
  • Not an "issue" that is how it works... but this is the correct answer none the less. Commented Feb 13, 2018 at 15:33
  • Thank you! Yes the problem was that the Add should be inside the ctx.Load Commented Feb 14, 2018 at 12:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.