kwizcom banner advertisement

SharePoint Online: Change List Field Settings using PowerShell

Requirement: Update SharePoint Online List Field Settings using PowerShell

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
$SiteURL="https://crescent.sharepoint.com/sites/projects"
$ListName="Projects"
$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
$List=$Ctx.Web.Lists.GetByTitle($ListName)

#Get the List Field
$Field=$List.Fields.GetByInternalNameOrTitle($FieldInternalName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#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
$Field.Update()
$Ctx.ExecuteQuery()

Write-host "Field Settings Updated!"

PnP PowerShell to Update Field Properties in SharePoint Online List
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$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"}  
SharePoint Online: Change List Field Settings using PowerShell SharePoint Online: Change List Field Settings using PowerShell Reviewed by Salaudeen Rajack on July 01, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.