I am trying to add read-only-permissions to a specific group called "Students" for a list I have created called "Quiz". I have to use PowerShell CSOM, and in every other tutorial I've been through, .NET server types have been used.
Code:
$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
}
}
}
My error is in
$roleAssignment = new-object Microsoft.SharePoint.Client.RoleAssignment($SiteGroup)
, where I'm recieving the error
Cannot find an overload for "RoleAssignment" and the argument coun
t: "1".
Most tutorials use
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($SiteGroup)
which I CAN NOT USE.
How can I complete my code?
P.S. I know my code is a bit messy, but I've been spending too much time trying to find a solution, and my code has greatly reduced in quality over the past hours. Sorry for that.