SharePoint Online: Set Content Type to Read Only using PowerShell

Requirement: Make a Content type to read only in SharePoint Online.

How to Make a Content Type to Read-Only in SharePoint Online?
To set a content type to read only:
  • Go to Settings >> Site settings
  • Click on "Site content types" link
  • From the content types listed, Click on the name of the content type
  • Go to Advanced settings in content type settings page. 
  • Set "Yes" for Should this content type be read only?"
sharepoint online powershell content type read only


SharePoint Online: PowerShell to Set Content Type to Read Only
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Business Contacts"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        
    #Get the content type from the web
    $ContentTypeColl = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypeColl)
    $Ctx.ExecuteQuery()
 
    #Get the content type to Add
    $CType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
    If($CType -ne $Null)
    {
        $CType.ReadOnly=$True
        $CType.Update($True)
        $Ctx.ExecuteQuery()

        Write-host "Content Type is Set to Read Only!" -ForegroundColor Green
    }
    else
    {
        Write-host "Content Type Doesn't Exist!" -ForegroundColor Yellow
    }
}
Catch {
    write-host -f Red "Error Setting Content Type to Read Only!" $_.Exception.Message
} 

PnP PowerShell to Make a Content Type Read Only:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type from site
$ContentType = Get-PnPContentType -Identity $ContentTypeName

If($ContentType)
{
    #Set the Content type to Read Only
    $ContentType.ReadOnly = $True
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Content Type '$ContentTypeName' is Set to Read Only!"
}
We can also set the content type to read only at list level:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$ListName ="Team Documents"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

If($ContentType)
{
    #Set the Content type to Read Only
    $ContentType.ReadOnly = $True
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Content Type '$ContentTypeName' is Set to Read Only!"
}
SharePoint Online: Set Content Type to Read Only using PowerShell SharePoint Online: Set Content Type to Read Only using PowerShell Reviewed by Salaudeen Rajack on April 27, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.