2015-06-29 40 views
1

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

代码:

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

,在那里我recieving错误

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

大多数教程使用

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

我无法使用。

我该如何填写我的代码?

P.S.我知道我的代码有点乱,但是我花了太多时间来寻找解决方案,并且过去几个小时我的代码质量大大降低。对不起。

回答

0

Unnie上stackexchange通过提供下面的代码帮助了我。我应该使用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() 
     } 
} 

This Works! :-)

0

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

反正你可以尝试的东西沿着这些路线:

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

$ctx.Load($list.RoleAssignments.Add($sitegroup, $roleAssignment)) 
相关问题