Sharepoint - SPO Retrieve HasUniqueRoleAssignements property using Powershell

The ClientRuntimeContext.Load method has the following signature:

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject

How to invoke ClientRuntimeContext.Load<T> method in PowerShell

Invoke-LoadMethod function demonstrates how to specify retrievals parameter for ClientRuntimeContext.Load<T> method in PowerShell:

Function Invoke-LoadMethod() {
param(
   [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
   [string]$PropertyName
) 
   $ctx = $Object.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $Object.GetType()
   $clientLoad = $load.MakeGenericMethod($type) 


   $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
   $Expression = [System.Linq.Expressions.Expression]::Lambda(
            [System.Linq.Expressions.Expression]::Convert(
                [System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),
                [System.Object]
            ),
            $($Parameter)
   )
   $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
   $ExpressionArray.SetValue($Expression, 0)
   $clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
}

Example

The following example demonstrates how to retrieve HasUniqueRoleAssignments property of List object:

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password

$list = $context.Web.Lists.GetByTitle($listTitle)    
Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments"
$context.ExecuteQuery()

Write-Host $list.HasUniqueRoleAssignments

$context.Dispose() 

References

  • Invoking Generic Methods on Non-Generic Classes in PowerShell
  • Some tips and tricks of using SharePoint Client Object Model in PowerShell. Part 1