Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
$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

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

, where I'm recieving the error

C#
Cannot find an overload for "RoleAssignment" and the argument coun
t: "1".


Most tutorials use

C#
$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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900