SharePoint Online: Set Content Type to Read Only using PowerShell
Requirement: Make a Content type read-only in SharePoint Online.
How to Make a Content Type to Read-Only in SharePoint Online?
In SharePoint Online, you can make a content type read-only to prevent users from editing it. This can be useful if you want to restrict who can modify a particular content type or if you wish to ensure that data is consistent. This article will show you how to make a content type read-only in SharePoint Online.
To set a content type to read-only, do the following:
- 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 the content type settings page.
- Set “Yes” for Should this content type be read only?”
SharePoint Online: PowerShell to Set Content Type to Read Only
What if you need to set the content type to read only through scripting? Let me show you how to use PowerShell to set the 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:
Setting a content type to read-only in SharePoint Online can be useful when you want to prevent users from making changes to specific content types, such as important documents or records. Here is how to set a content type to read only using PnP PowerShell.
#Config Variables
$SiteURL = "https://Crescent.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 the list level:
#Config Variables
$SiteURL = "https://Crescent.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!"
}
How do I set the List ContentType to NOT read-only then delete after? Thanks
Thanks Salaudeen Rajack. Being a newbie to sharepoint, I search for even small things on google. Particularly when its powershell scripts.. ‘Sharepointdiary’ is the first link I look for as I found solutions to many of my problems here. Thanks a lot for sharing your knowledge.
I’m glad it helped! Thanks.