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.
Check if List Exists in SharePoint Online using PowerShell

PowerShell Script to check if List Exists in SharePoint Online:
Here is the SharePoint Online PowerShell check if 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
 }

Method 2: PowerShell to Check If Lists Exists in SharePoint Online
Instead of "Try-Catch" method, lets check if the site has given 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!"
}
SharePoint Online: Check if List Exists using PowerShell SharePoint Online: Check if List Exists using PowerShell Reviewed by Salaudeen Rajack on October 08, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.