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

时间:2015-06-29 09:20:34

标签: powershell permissions roles csom

我正在尝试为名为“学生”的特定组添加只读权限,以便创建名为“测验”的列表。我必须使用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 个答案:

答案 0 :(得分:0)

我没有要测试的sharepoint,但是你在角色分配上遇到了错误,因为你调用的方法需要2个参数。

无论如何,你可以尝试这些方法:

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

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

答案 1 :(得分:0)

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

这个有效! : - )