SharePoint Online: Check If a Field Exists in List using PowerShell

Requirement: PowerShell to check if a field exists in a list in SharePoint Online.

SharePoint Online – PowerShell to check if a Column exists in List

In some cases, you may need to determine if a field exists in a list before creating a new field in the list while automating tasks. In this blog post, we will look at how to use PowerShell to check if a field exists in a SharePoint Online list.

check if a column exists in sharepoint online list

Here is the PowerShell to check if a field exists in a 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"
  
#Function to Check if a column exists in a List
Function Check-ColumnExists($SiteURL, $ListName, $FieldName, $Credentials)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Retrive all Fields from the list
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Fields = $List.Fields
    $Ctx.Load($List)
    $Ctx.Load($Fields)
    $Ctx.ExecuteQuery()

    #Check if the given field name 
    $Field = $Fields | where{$_.Title -eq $FieldName}
    if($Field)  { return $true } else { return $false}
}

#Set Variables for Site URL, List Name and Column Name
$URL= "https://crescent.sharepoint.com/sites/sales/"
$List="Project Documents"
$Field="Department" #Display Name

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to Check Column Exists in given list
$ColumnExists = Check-ColumnExists -SiteURL $URL -ListName $List -FieldName $Field -Credentials $Cred

if($ColumnExists) {
    write-host "Column Exists in Given List!" -f Green
    #Proceed with your script
 }
 else {
    write-host "Column Doesn't Exists in given List!" -f Red
 }

PnP PowerShell to check if field exists in SharePoint Online List

If you’re doing some automation in SharePoint Online, you may need to determine whether a particular field exists in your list or library. You can use PnP PowerShell to determine if a field exists in a list and then add the field to a list.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName ="Projects"
$FieldTitle = "Project Classification"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
    
#Get the column from list
$Field = Get-PnPField -Identity $FieldTitle -List $ListName -ErrorAction SilentlyContinue

If($Field -eq $Null)
{
    Write-host "Field '$FieldTitle' does not exists in the list '$ListName'" -f Yellow
}
Else
{
    Write-host "Field '$FieldTitle' exists in the list '$ListName'" -f Green
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply