使用 Powershell CSOM 将只读权限添加到 Sharepoint 组的列表

Adding Read-Only-permissions to a list for a Sharepoint Group using Powershell CSOM

我正在尝试将只读权限添加到名为 "Students" 的特定组,用于我创建的名为 "Quiz" 的列表。我必须使用 PowerShell CSOM,但在我完成的所有其他教程中,都使用了 .NET 服务器类型,这不适用于我的代码。

代码:

$ListName = "Quiz"
$PermissionLevel = "Read Only"
$web = $ctx.Web        

$lists = $web.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
foreach($list in $lists)
{
    if($list.Title -eq $ListName)
    {
        $listId = $list.Id
    }
}
$list = $lists.GetById($listId)
$ctx.Load($list);
$ctx.ExecuteQuery();

Write-Host "List:" $List.Title -foregroundcolor Green
if ($list -ne $null)
{
    $groups = $web.SiteGroups
    $ctx.Load($groups)
    $ctx.ExecuteQuery()
    foreach ($SiteGroup in $groups) {                    
        if ($SiteGroup.Title -match "Students")
        {
            write-host "Group:" $SiteGroup.Title -foregroundcolor Green
            $GroupName = $SiteGroup.Title

            $builtInRole = $ctx.Web.RoleDefinitions.GetByName($PermissionLevel)

            $roleAssignment = new-object Microsoft.SharePoint.Client.RoleAssignment($SiteGroup)
            $roleAssignment.Add($builtInRole)

            $list.BreakRoleInheritance($True, $False)
            $list.RoleAssignments.Add($roleAssignment)
            $list.Update();
            Write-Host "Successfully added <$GroupName> to the <$ListName> list in <$site>. " -foregroundcolor Green
        }                
        else
        {
                Write-Host "No Students groups exist." -foregroundcolor Red
        }
    }
}

我的错误在

$roleAssignment = new-object Microsoft.SharePoint.Client.RoleAssignment($SiteGroup)

,我收到错误

Cannot find an overload for "RoleAssignment" and the argument count: "1".

大多数教程使用

$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($SiteGroup)

我不能使用。

如何完成我的代码?

P.S。我知道我的代码有点乱,但我花了太多时间试图找到解决方案,而且在过去的几个小时里我的代码质量大大降低了。抱歉。

我没有要测试的共享点,但您收到角色分配错误,因为您调用的方法有 2 个参数。

无论如何,您可以尝试以下方法:

$roleAssignment = New-Object microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$roleAssignment.Add($builtinRole)

$ctx.Load($list.RoleAssignments.Add($sitegroup, $roleAssignment))
stackexchange 上的

Unnie 通过提供以下代码帮助了我。我应该使用 microsoft.SharePoint.Client.RoleDefinitionBindingCollection 而不是 Microsoft.SharePoint.Client.RoleAssignment

# Get the list by Title and load.
$web = $ctx.Web 
$list = $web.Lists.GetByTitle("Quiz")
$ctx.Load($list)
 # Load in list of groups on the current web.
$groups = $web.SiteGroups
$ctx.Load($groups)

$ctx.ExecuteQuery()

$listTitle = $list.Title

foreach($group in $groups)
{
        if($group.Title -eq "Students")
        {
                $roleAssignment = $null 
                # Get the group and load into context to be used.
                $StudentsGrp = $groups.GetById($group.Id)
                $ctx.Load($StudentsGrp)
                $ctx.ExecuteQuery()

                # Break inheritance on the list and remove existing permissons.
                $list.BreakRoleInheritance($false, $true)

                # Get the permissions role for 'Read'
                $reader = $web.RoleDefinitions.GetByName("Read")

                # Create a role assignment and apply the 'read' role.
                $roleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
                $roleAssignment.Add($reader)

                # Apply the  permission roles to the list.
                $ctx.Load($list.RoleAssignments.Add($StudentsGrp, $roleAssignment))
                $list.Update()
                $ctx.ExecuteQuery()
        }
}

这行得通! :-)