Saturday, October 8, 2016

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:

#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
 }



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...