Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Created May 11, 2015 15:03
Show Gist options
  • Save asadrefai/1828f936fa806c89b098 to your computer and use it in GitHub Desktop.
Save asadrefai/1828f936fa806c89b098 to your computer and use it in GitHub Desktop.
Delete a list view of a SharePoint list using CSOM PowerShell
function Delete-ListView()
{
param(
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$listName,
[Parameter(Mandatory=$true)][string]$viewName
)
begin{
try
{
#get Client Object
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $credentials
#Retrieve List
$list = $context.Web.Lists.GetByTitle($listName)
$context.Load($list)
$context.ExecuteQuery()
}
catch
{
Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red
}
}
process{
try
{
$view = $list.Views.GetByTitle($viewName)
$context.Load($view)
$context.ExecuteQuery()
$view.DeleteObject()
$context.ExecuteQuery()
Write-Host "View deleted successfully" -ForegroundColor Green
}
catch
{
Write-Host ("Error while deleting view for a 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