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?
Are you looking to make a SharePoint Online list field read only? Perhaps you’ve found no ways to do this through the UI, but wonder if PowerShell can help. In this blog post, we’ll look at how to make a SharePoint Online list field read only 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://Crescent.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!
PnP PowerShell to Set a List Field to Read Only
You can also use PnP PowerShell to make a field to read-only in a SharePoint Online:
#Config Variables
$SiteURL = "https://Crescent.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()
works but Readonlyfield set to true hides the field in Display/Edit/New forms