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.

PowerShell to check if a Column exists in a 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

If you need to check if a column exists in a list using PowerShell, there are several ways to do it. Here is the CSOM 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
 }

This PowerShell script provides a simple and effective way to check if a column exists in a SharePoint Online list by checking all available fields of the list.

PnP PowerShell to check if a 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 the PnP PowerShell cmdlet Get-PnPField 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
}

In summary, there are several ways to check if a column exists in a list using PowerShell. You can use the Get-PnPField cmdlet or the CSOM PowerShell to check if a column exists by checking the Fields of the list. By using these cmdlets, you can quickly and easily check if a column exists in a list and perform the necessary actions.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *