Created
May 11, 2015 14:52
-
-
Save asadrefai/57df4d77692a0bfbb582 to your computer and use it in GitHub Desktop.
Check if SharePoint list exists using PowerShell
This file contains 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 Check-ListExists() | |
{ | |
param( | |
[Parameter(Mandatory=$true)][string]$url, | |
[Parameter(Mandatory=$true)][System.Net.NetworkCredential]$credentials, | |
[Parameter(Mandatory=$true)][string]$listName | |
) | |
begin{ | |
try | |
{ | |
#get Client Object | |
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url) | |
$context.Credentials = $credentials | |
$web = $context.Web | |
$lists = $web.Lists | |
$context.Load($web) | |
$context.Load($lists) | |
$context.ExecuteQuery() | |
} | |
catch | |
{ | |
Write-Host ("Error while getting context. Error -->> " + $_.Exception.Message) -ForegroundColor Red | |
} | |
} | |
process{ | |
try | |
{ | |
$list = $web.Lists | where{$_.Title -eq $listName} | |
if($list) | |
{ | |
#Execute if list exists | |
Write-Host "List " $listName " exists" -ForegroundColor Green | |
return $true | |
} | |
else | |
{ | |
#Execute if list does not exists | |
Write-Host "List " $listName " does not exists" -ForegroundColor Green | |
return $false | |
} | |
} | |
catch | |
{ | |
Write-Host ("Error while checking for list. 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