kwizcom banner advertisement

SharePoint Online: Check if Site Collection, Subsite Exists using PowerShell CSOM

Requirement: Check if site or site collection exists in SharePoint online using PowerShell CSOM.

SharePoint Online PowerShell-CSOM to check if site or site collection exists in given URL:
This PowerShell script checks if site collection exists in the given URL.

#Load SharePoint online 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-SiteExists($SiteURL, $Credentials)
{
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.Web
    $Ctx.Load($web)
    
    Try {
            $Ctx.ExecuteQuery()
            Return $True
        }
    Catch [Exception] {
      Write-host $_.Exception.Message -f Red
      Return $False
     }        
}

#Variable for Site collection URL
$URL= "https://crescent.sharepoint.com/sites/sales/"

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

#Call the function to check site exists
if($SiteExists -eq $True)
 {
    write-host "Given Site Collection Exists!" -f Green
    #Proceed with your script
 }
 else
 {
    write-host "Site Collection doesn't Exists on given URL!" -f Red
 }
Please note, the function Check-SiteExists may return false in case of any network related issue or authentication failures.

My another server side script to Check if site collection or subsite exists in SharePoint using PowerShell in SharePoint on-premises.

Tags: sharepoint check site exists, sharepoint check site collection exists, powershell sharepoint check site exists, sharepoint csom check if site exists, sharepoint check if url valid, sharepoint powershell check if site exists, check if subsite exists sharepoint powershell csom
SharePoint Online: Check if Site Collection, Subsite Exists using PowerShell CSOM SharePoint Online: Check if Site Collection, Subsite Exists using PowerShell CSOM Reviewed by Salaudeen Rajack on 11:40 AM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.