Powershell client context

Harish Patil 21 Reputation points
2021-02-08T09:03:44.997+00:00

Hi Team,

I am using below code. I want to check is site collection is read only or not.

How to check site is read only through powershell client context.

Code:

$web = $clientContextSource.Web;
$clientContextSource.Load($web);
$clientContextSource.ExecuteQuery();

LogInfo "ClientContext initialized for Source, site title: $($web.Url)";

Thanks,
Harish Patil

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,623 questions
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2021-02-09T03:07:22.207+00:00

    Hi @Harish Patil ,

    Please use ClientContext.Site property to check if site is read-only, please refer the following script:

    #Variables for Processing  
    $SiteUrl = "https://TenantName.sharepoint.com/sites/SiteName/"  
       
    $UserName="user@TenantName.onmicrosoft.com"  
    $Password ="*******"  
        
    #Setup Credentials to connect  
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
        
    #Set up the context  
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
    $Context.Credentials = $credentials  
    $Context.Load($Context.Site)  
    $Context.ExecuteQuery()  
      
      
    Write-Host "Checking Site ReadOnly" - $Context.Site.ReadOnly   
    

    65622-snipaste-2021-02-09-11-05-35.png

    Reference:

    Site.ReadOnly property

    Thanks
    Best Regards


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. sadomovalex 3,631 Reputation points
    2021-02-08T15:46:34.27+00:00

    you need to load Site.ReadOnly property of appropriate site collection. You may do this by using helper PowerShell method Load-CSOMProperties of Garry Lapointe:

    Load-CSOMProperties -object $site -propertyNames @("ReadOnly")  
    

    Here is example of usage of this method: PowerShell script for listing master pages in all sites of Sharepoint Online site collection via CSOM.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.