Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Created August 11, 2015 09:20
Show Gist options
  • Select an option

  • Save asadrefai/20ebe22656a04996453b to your computer and use it in GitHub Desktop.

Select an option

Save asadrefai/20ebe22656a04996453b to your computer and use it in GitHub Desktop.
Check if SharePoint Publishing Page Exists using CSOM PowerShell
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