SharePoint Office 365 - How to Get the Lists with Unique Permission – CSOM PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   13 Mar 2018
Visit Count
Today :  1    Total :   1451



In this earlier article, We saw how to get the lists having unique permission using C#. In the same manner, let us see, how to get the unique permission lists using PowerShell.

The script is straight forward and does not require much explanation.

 

 cls
 
 #Import the required client dlls
 Import-Module   'C:\SATHISH\PRACTICE SOURCE CODES\Office365.Console\packages\Microsoft.SharePointOnline.CSOM.16.1.6420.1200\lib\net45\Microsoft.SharePoint.Client.dll'
 Import-Module   'C:\SATHISH\PRACTICE SOURCE CODES\Office365.Console\packages\Microsoft.SharePointOnline.CSOM.16.1.6420.1200\lib\net45\Microsoft.SharePoint.Client.Runtime.dll'
 
 #Generic method to load the properties
 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))
 }
 
 
 #Mysite URL
 $site = 'https://sppalsmvp.sharepoint.com/sites/DeveloperSite/'
 
 #Admin User Principal Name
 $admin = 'sathish@sppalsmvp.OnMicrosoft.Com'
 
 #Get Password as secure String
 $password = Read-Host 'Enter Password' -AsSecureString
 
 #Get the Client Context and Bind the Site Collection
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
 
 #Authenticate
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
 $context.Credentials = $credentials
 
 $list = $context.Web.Lists.GetByTitle('D1')
 $context.Load($list)
  
 
 Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments"
 
 $context.ExecuteQuery()
 
 Write-Host $list.HasUniqueRoleAssignments 
 

Happy Coding,

Sathish Nadarajan.

Categories