获得通用"收集未加载"尝试将组添加到库权限时CSOM powershell上的错误

时间:2017-09-07 09:32:23

标签: powershell sharepoint-online csom

我正在尝试将一个组添加到库权限。我的代码如下:

#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)

我已经尝试了我能想到的一切,我甚至手动完成了代码,我仍然无法找到问题,我已经在每个阶段调试输出并确认它们的值是在最终代码段中使用的所有3个变量。

非常感谢任何帮助/建议。

1 个答案:

答案 0 :(得分:1)

我做的一些事情与你的脚本略有不同。我参考了$web

中的角色定义
$roleDefContributor = $web.RoleDefinitions.GetByName("Contribute")

围绕$ctx.Load(..)中的角色分配:

$ctx.Load($collRoleAssign.Add($MembersGroup, $ContributorRoleDefBinding))

工作示例:

$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()