Reputation: 515
I am attempting to add a group to library permissions. My code is below:
#earlier in my code the list is created so it's already stored at this point, Breaking Inheritance is successful
$myList.BreakRoleInheritance($false,$true)
$myList.Update()
$Ctx.ExecuteQuery()
$GroupnameMembers="Site Members"
$roleDefs = $Ctx.Web.RoleDefinitions
$webgroups = $Ctx.Web.SiteGroups
$Ctx.Load($roleDefs)
$Ctx.Load($webgroups)
$Ctx.ExecuteQuery()
$roleTypeContributor = [Microsoft.SharePoint.Client.RoleType]"Contributor"
$roleDefContributor = $roleDefs | where {$_.RoleTypeKind -eq $RoleTypeContributor}
$MembersGroup = $webgroups | Where{$_.Title -eq $GroupnameMembers}
$ContributorRoleDefBinding = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
$ContributorRoleDefBinding.Add($roleDefContributor)
#earlier in my code the list is created so it's already stored at this point
$collRoleAssign = $myList.RoleAssignments
$Ctx.Load($collRoleAssign)
$Ctx.ExecuteQuery()
#Crashing on this line below:
$collRoleAssign.Add($MembersGroup, $ContributorRoleDefBinding)
I've tried everything I can think of, I have even manually stepped through the code and I still can't find the issue, I have thrown in Debugging outputs at each stage and confirmed their are values in all 3 variables being used in that final code piece.
Any help/suggestions would be greatly appreciated.
Upvotes: 0
Views: 126
Reputation: 477
There are a couple of things that I do slightly differently to your script. I get reference to the role definition from the $web
:
$roleDefContributor = $web.RoleDefinitions.GetByName("Contribute")
And surround the role assignment in a $ctx.Load(..)
:
$ctx.Load($collRoleAssign.Add($MembersGroup, $ContributorRoleDefBinding))
Working example:
$list = $web.Lists.GetByTitle($listName)
$ctx.Load($list)
# break inheritance
$list.BreakRoleInheritance($false, $true)
$groupName = "Site Members"
$webgroups = $ctx.Web.SiteGroups
$ctx.Load($webGroups)
$ctx.ExecuteQuery()
$roleDef = $web.RoleDefinitions.GetByName("Contribute")
$group = $webGroups | Where{ $_.Title -eq $groupName }
$roleDefBinding = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$roleDefBinding.Add($roleDef)
$ctx.Load($list.RoleAssignments.Add($group, $roleDefBinding))
$list.Update()
$ctx.ExecuteQuery()
Upvotes: 1