Updating SharePoint list properties manually can be a time-consuming process, especially when dealing with a large number of lists or fields. This script automates the process, saving time and reducing the risk of errors.

In this blog post, we’ll delve into a PowerShell script that automates the process of updating a field in a SharePoint list to be required. This script is particularly useful when you need to enforce data integrity by ensuring that certain fields are always filled out.

PowerShell Script to update List Field value

Here’s the script we’ll be discussing:

#connect to sharepoint online site
Connect-PnPOnline -Url "https://mstalk.sharepoint.com" -Interactive

$fieldName = "Email"
$listName = "ContactDetils"

#get the current web
$spWeb = Get-PnPWeb 

#this will update the SharePoint list property
if($spWeb -ne $null)
{
    #fetch the list field
    $spField = Get-PnPField -List $listName -identity $fieldName
    #This will update the field to 
    $spField.required = $true
    $spField.update()
    $spField.context.executeQuery()
}

This PowerShell script is used to connect to a SharePoint Online site and updates a field in a SharePoint list to be required. Here’s a detailed breakdown:

  1. Connect-PnPOnline -Url "https://mstalk.sharepoint.com" -Interactive: This line connects to a SharePoint Online site using interactive login.
  2. $fieldName = "Email" and $listName = "ContactDetils": These lines set the name of the field and the list that you want to update.
  3. $spWeb = Get-PnPWeb: This line gets the current SharePoint web (site).
  4. The if($spWeb -ne $null) block checks if the SharePoint web exists.
  5. Inside the if block:
    • $spField = Get-PnPField -List $listName -identity $fieldName: This line gets the field from the specified list.
    • $spField.required = $true: This line sets the field to be required.
    • $spField.update() and $spField.context.executeQuery(): These lines update the field in SharePoint.

By using this script, administrators can ensure that all specified fields are updated with the correct settings. This is particularly useful in large organizations where there may be a need to update multiple fields across various lists.

Adnan, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose.Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies.Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply