SharePoint Online: How to Make List Field Read Only using PowerShell?

Requirement: Make a Field Read Only in SharePoint Online List

How to Set a List Field to Read Only in SharePoint Online using PowerShell?
#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"

#Function to set a Field to Read Only in SharePoint Online List
Function Set-SPOFieldReadOnly($SiteURL, $ListName, $FieldInternalName, [Bool]$IsReadOnly)
{
    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 List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Get the Field
        $Field = $List.Fields.GetByInternalNameOrTitle($FieldInternalName)
        $Field.ReadOnlyField = $IsReadOnly
        $Field.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Read Only Settings Update for the Field Successfully!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Set parameter values
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Team Documents"
$FieldInternalName = "Classification" #Internal Name
$IsReadOnly = $True

Set-SPOFieldReadOnly -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName -IsReadOnly $IsReadOnly
This sets the field to read only even in Quick Edit mode!
sharepoint online make list field read only

PnP PowerShell to Set a List Field to Read Only
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Team Documents"
$ColumnName = "Classification" #Internal Name

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

#Get the Context
$Context = Get-PnPContext

#Get the Field from List
$Field = Get-PnPField -List $ListName -Identity $ColumnName

#Set the Field to Read Only
$Field.ReadOnlyField = $True
$Field.Update() 
$Context.ExecuteQuery() 
SharePoint Online: How to Make List Field Read Only using PowerShell? SharePoint Online: How to Make List Field Read Only using PowerShell? Reviewed by Salaudeen Rajack on April 10, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.