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.
There are a couple of things that I do slightly differently to your script. I get reference to the role definition from the
$web
:And surround the role assignment in a
$ctx.Load(..)
:Working example: