SharePoint Online: Change List Field Settings using PowerShell

Requirement: Update SharePoint Online List Field Settings using PowerShell

How to Edit Column Settings in SharePoint Online?
Existing columns of a list or library can be edited. Properties of the fields such as display name, type, or other related column settings can be updated through column settings page. To change column settings in a list or library:
  • Navigate to the SharePoint Online list or library where you want to edit a column.
  • Go to the list or library settings page either from Ribbon or from Site Settings menu >>Under the Columns section, Pick the column to update
  • On the Edit Column page, You can make any necessary changes and click on OK button to Save.
    SharePoint Online update List Field Settings using PowerShell
Please note, changing column type is limited within specific columns based on the current type of the column. Also, you may end up  in data loss when switching between column type. E.g. When you switch from Multiple lines of text to Single line of text, Its limited to 255 characters.

PowerShell to Set Field Settings in SharePoint Online List:
#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"

#Set Config Parameters
$FieldName="HeadCount" #Internal Name

#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

#Get the List Field

#Set List Field Settings
$Field.Title= "Total Resources"
$Field.Description = "Number of Resources working on the Project"

#set default value
$Field.DefaultValue = "10"

$Field.EnforceUniqueValues = $False

#set field hidden
$Field.Hidden = $False
$Field.Sealed = $False

#set field required
$Field.Required = $True
$Field.FieldTypeKind = "Text"
$Field.Indexed = $False

#set field read only
#$Field.ReadOnlyField = $True

#Apply changes

Write-host "Field Settings Updated!"

PnP PowerShell to Update Field Properties in SharePoint Online List
#Config Variables
$SiteURL = ""
$ListName = "Team Projects"
$FieldName = "ProjectStatus" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    #Set Field Properties
    Set-PnPField -List $ListName -Identity $FieldName -Values @{Required=$True;DefaultValue="Active"}    
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
Similarly, you can set any other property of the list field such as "Title" or "Description" as:
-Values @{Title="Name of the Column";Description="Updated List Description"}  

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.