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 field required in SharePoint, or to remove required field (to make it non-mandatory) in SharePoint list:
  • 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. 
sharepoint powershell set field required

PowerShell to Set a Field Required in SharePoint Online List:
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://crescenttech.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 required field setting for multiple fields in SharePoint list, use:
#Set parameter values
$SiteURL="https://crescenttech.sharepoint.com/"
$ListName="Projects"
#Set Field Internal Names to make required or disable required
$FieldNames=@("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://crescenttech.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
}

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="http://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).
How to Make a Field Required in SharePoint List using PowerShell? How to Make a Field Required in SharePoint List using PowerShell? Reviewed by Salaudeen Rajack on January 21, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.