How to Make a Field Required in SharePoint List using PowerShell?
Requirement: Make a Field Required in SharePoint Online List.
How to Set a List Field Required in SharePoint?
To make a field required in SharePoint, or to remove the required field (to make it non-mandatory) in the SharePoint list, follow these steps:
- Go to List Settings >> Pick the Field by Clicking on its “Title” under “Columns” Section
- Set “Yes” for “Require that this column contains information”
- Click “OK” to save changes. This makes the field required.
PowerShell to Set a Field Required in SharePoint Online List:
Do you want to make a field required in a SharePoint list using PowerShell? This can be done by setting the Required Property of the field to $true. In this blog post, we will show you how to do this. We will also provide a script that you can use to make all fields in a SharePoint list required. Happy scripting!
Here is the PowerShell to set a column as “required”:
#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 parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"
#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($FieldName)
$Field.Required = $True
$Field.Update()
$Ctx.ExecuteQuery()
Write-host "Field 'Required' Settings Updated!"
This makes the field required in SharePoint. Similarly, To remove the required field setting for multiple fields in the SharePoint list, use:
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
#Set Field Internal Names to make required or disable required
[email protected]("StartDate","ProjectID","Category","Budget")
#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)
#Iterate through each field given
ForEach($FieldName in $FieldNames)
{
#Get the Field
$Field = $List.Fields.GetByInternalNameOrTitle($FieldName)
#disable "required" setting for the field
$Field.Required = $False
$Field.Update()
$Ctx.ExecuteQuery()
Write-host "Field '$FieldName' Required Settings Updated!"
}
PnP PowerShell to Set a List Column Required in SharePoint Online
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Projects"
$FieldName= "ProjectStatuss" #Internal Name
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get the Field from List
$Field = Get-PnPField -List $ListName -Identity $FieldName -ErrorAction Stop
#Set the Field Required
$Field.Required = $True
$Field.Update()
$Field.Context.ExecuteQuery()
Write-host -f Green "Field '$FieldName' Set to Mandatory!"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
How about setting all columns required?
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all fields from the list
Get-PnPField -List $ListName | Where { $_.Required -eq $False -and $_.Title -ne "Name" -and $_.ReadOnlyField -eq $false -and `
$_.Hidden -eq $false -and $_.InternalName -ne "ContentType" -and $_.InternalName -ne "Attachments"} | Set-PnPField -Values @{Required=$True}
Similarly, you can remove required flag for all fields in SharePoint Online using:
Get-PnPField -List $ListName | Where { $_.Required -eq $True -and $_.Title -ne "Name" -and $_.ReadOnlyField -eq $false -and $_.Hidden -eq $false -and $_.InternalName -ne "ContentType" -and $_.InternalName -ne "Attachments"} | Set-PnPField -Values @{Required=$False}
Disable Required Field in SharePoint On-Premises:
If you need to remove required flag for a field in SharePoint list, use this PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Set Variables
$SiteURL="https://intranet.crescent.com"
$ListName="Projects"
$FieldName="StartDate"
#Get Web, List and Field Objects
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists[$ListName]
$Field = $List.Fields[$FieldName]
#Set Required to False
$Field.Required = $False
$Field.Update()
Write-host "Field Required Setting updated!"
This removes required field in SharePoint list (makes the field as non-mandatory).
After a New Item form is already in use, can you go back and change certain fields to be required going forward without impacted the forms that were already completed previously?
This doesn’t seem to be available for lookup columns? We had lookup columns required since Office 365 was released. Today I noticed that those fields are no longer required. Is that a change?