CSOM PowerShell function to check if the a SharePoint Online site exits? It accepts credential and Url and return true or false.





$siteUrl = 'https://spo.sharepoint.com/sites/teamsite'
$credential = Get-Credential;

function CheckSiteExists($credential,$siteUrl){

 Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll" 
 Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll" 

     $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
     $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName,$credential.Password);
     [System.Net.WebRequest]::DefaultWebProxy.Credentials = $Creds # needed for default gateway.
     $ctx.Credentials = $Creds;
     $web = $ctx.Web
      $ctx.Load($web)
      try{
            $ctx.ExecuteQuery()         
            return $true
         }
         catch{           
            return $false
        
         }  
        
}

#Calling the function: CheckSiteExits  $credentials $siteUrl

No comments:

Post a Comment