Created
August 11, 2015 09:20
-
-
Save asadrefai/20ebe22656a04996453b to your computer and use it in GitHub Desktop.
Check if SharePoint Publishing Page Exists using CSOM PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function CheckPageExists() | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$siteurl, | |
| [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials, | |
| [Parameter(Mandatory=$false)][string]$PageName | |
| ) | |
| begin{ | |
| try | |
| { | |
| $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl) | |
| $context.Credentials = $credentials | |
| $Pages = $context.Web.Lists.GetByTitle('Pages') | |
| $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery | |
| $camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+ $PageName +'</Value></Eq></Where></Query></View>' | |
| $Page = $Pages.GetItems($camlQuery) | |
| $context.Load($Page) | |
| $context.ExecuteQuery() | |
| } | |
| catch | |
| { | |
| Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red | |
| } | |
| } | |
| process{ | |
| try | |
| { | |
| $file = $Page.File | |
| if($file) | |
| { | |
| #Execute if Page exists | |
| Write-Host "Page exists" -ForegroundColor Green | |
| return $true | |
| } | |
| else | |
| { | |
| #Execute if Page does not exists | |
| Write-Host "Page does not exists" -ForegroundColor Green | |
| return $false | |
| } | |
| } | |
| catch | |
| { | |
| Write-Host ("Error while checking for Page. Error -->> " + $_.Exception.Message) -ForegroundColor Red | |
| } | |
| } | |
| end{ | |
| $context.Dispose() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment