kwizcom banner advertisement

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 Conent Type to Read Only!" $_.Exception.Message
} 
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.