0
votes

I have problems to list lists permissions using CSOM/PowerShell.

Variables / filters

$spSiteUrl = "https://mytenant.sharepoint.com"

Getting credentials

if($cred -eq $null)
{
    $cred = Get-Credential
}

Loading Assemblies

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

Connecting into SharePoint and showing site title

Write-Host "Connecting to SharePoint"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($spSiteUrl) 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)

$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()
Write-host "Site Name : $($web.Title)"

Function listing "useful" applications

function getApps($web)
{
    $appsArray = @()

    $apps = $web.Lists
    $ctx.Load($apps)   

    $ctx.ExecuteQuery()  

    Write-Host "List of aplications : "
    foreach($app in $apps){  
        if($app.Hidden -eq $false)
        {
            $item = New-Object PSObject
            $item | Add-Member -MemberType NoteProperty -Name 'Col1' -Value $($app.Title)
            $item | Add-Member -MemberType NoteProperty -Name 'Col2' -Value $($app.HasUniqueRoleAssignments)
            $item | Add-Member -MemberType NoteProperty -Name 'Col3' -Value $($app.RoleAssignments)
            $item | Add-Member -MemberType NoteProperty -Name 'Col4' -Value $($app.BrowserFileHandling)
            $item | Add-Member -MemberType NoteProperty -Name 'Col5' -Value $($app.EffectiveBasePermissions)
            $item | Add-Member -MemberType NoteProperty -Name 'Col6' -Value $($app.Fields)
            $item | Add-Member -MemberType NoteProperty -Name 'Col7' -Value $($app.WorkflowAssociations)
            $appsArray += $item
        }
    } 
    $appsArray | Format-Table
}

Calling the function

getApps($web)

My problem is that :

  • $app.HasUniqueRoleAssignments
  • $app.RoleAssignments
  • $app.BrowserFileHandling
  • $app.EffectiveBasePermissions
  • $app.Fields
  • $app.WorkflowAssociations

Return me errors

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested..

1

1 Answers

0
votes

The exception

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Typically means that the property you are trying to work with (e.g. HasUniqueRoleAssignments) was not retrieved from the server yet.

You probably need an additional ExecuteQuery to load each app

foreach($app in $apps){  
if($app.Hidden -eq $false)
{
 $ctx.Load($app)
 $ctx.ExecuteQuery()

You will eventually notice that some properties can't be retrieved with regular csom api (such as HasUniqueRoleAssignments) and for those you can use Gary's powershell, giving you the possibility to do what you otherwise would use linq

foreach($app in $apps){  
if($app.Hidden -eq $false)
{
 $ctx.Load($app)
 Load-CSOMProperties -object $app -propertyNames @("HasUniqueRoleAssignments")
 $ctx.ExecuteQuery()

https://gist.github.com/glapointe/cc75574a1d4a225f401b#file-load-csomproperties-ps1

https://sharepoint.stackexchange.com/questions/126221/spo-retrieve-hasuniqueroleassignements-property-using-powershell