SharePoint Online: Check If List Exists using PowerShell
Requirement: Check if a particular list exists in a given SharePoint Online site using PowerShell CSOM script.
PowerShell Script to check if List Exists in SharePoint Online:
Do you need to check if a SharePoint list or document library exists before creating it? PowerShell can help! In this post, we’ll show you how to use PowerShell to determine whether a list exists or not. It will search for the list by name and return a message indicating whether the list was found or not.
Here is the SharePoint Online PowerShell to check if a list exists:
#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"
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-ListExists($SiteURL, $ListName, $Credentials)
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
Try {
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
Return $True
}
Catch [Exception] {
Write-host $_.Exception.Message -f Red
Return $False
}
}
#Set Variables for Site URL and List Name
$URL= "https://crescent.sharepoint.com/sites/sales/"
$List="Documents"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Call the function to Check List Exists in given web
$ListExists = Check-ListExists -SiteURL $URL -ListName $List -Credentials $Cred
if($ListExists) {
write-host "List Exists in Given Site!!" -f Green
#Proceed with your script
}
else {
write-host "List Doesn't Exists on given web!" -f Red
}
This can be useful if you want to automate tasks or build workflows that need to check for the existence of a file before taking certain actions.
Method 2: PowerShell to Check If Lists Exist in SharePoint Online
Instead of the “Try-Catch” method, let’s check if the site has given a list using the list name.
#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 list or document library exists
Function Check-SPOListExists([Microsoft.SharePoint.Client.ClientContext]$Ctx, [String]$ListName)
{
Try {
#Get All lists from the web
$Lists = $Ctx.Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Check if the given List exists
$List = $Lists | where {$_.Title -eq $ListName}
If($List -ne $Null)
{
Return $True
}
Else
{
Return $False
}
}
Catch {
Write-host -f Red $_.Exception.Message
Return $False
}
}
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Project Documents"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
#Call the function to Check List Exists in given web
$ListExists = Check-SPOListExists -Ctx $Ctx -ListName $ListName
If($ListExists -eq $True)
{
Write-Host -f Green "List '"$ListName"' exists!"
#Do Something with the List
#$List.Recycle()
#$Ctx.ExecuteQuery()
}
Else
{
Write-Host -f Red "List '"$ListName"' does not exists!"
}
PnP PowerShell to Check If List Exists in SharePoint Online
One of the easiest ways to check if a list exists in SharePoint Online using PowerShell is to use the Get-PnPList cmdlet. Let me show you how to use PnP PowerShell to determine if a list exists and then create it if necessary:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Tasks"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Try to Get the List or document library
$List = Get-PnPList -Identity $ListName -ErrorAction SilentlyContinue
If($List -ne $Null)
{
Write-Host -f Green "List Exist!"
}
Else
{
Write-Host -f Yellow "List Does Not Exist!"
#Create the list
}
If the list exists, you will see “List exists”. If the list does not exist, you will see the “List does not exist” message.
In conclusion, there are several ways to check if a list exists in SharePoint Online using PowerShell. You can use the Get-PnPList cmdlet, CSOM PowerShell script, or use the Try-Catch method to check if a list exists. By using these scripts, you can quickly and easily check if a list exists.