Thursday, August 18, 2016

SharePoint Online: Delete List View using PowerShell

How to delete a view in sharepoint online?
To delete a list view in SharePoint online, follow these steps:

  • Login to your SharePoint online site, Navigate to the list
  • Select the View to delete. Click on Modify this View (Or go to list settings, Click on the respective view title)
  • On Edit view page, Click on "Delete" button. Confirm the prompt once to delete the view from SharePoint online list or library.
    How to delete a view in sharepoint online
 Delete a view in SharePoint Online using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteURL="https://crescent.sharepoint.com"
$ListName= "Projects"
$ViewName="Active Projects"

Try {
    #Get Credentials to connect
    #$Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)

    #Get the view to delete
    $View = $List.Views.getByTitle($ViewName)
    
    #Delete the list view
    $View.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
 }
Catch {
    write-host -f Red "Error Deleting List View!" $_.Exception.Message
}

PowerShell Script to Delete a List view in SharePoint Online:
The above script can be wrapped into a re-usable function. Here is how:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-ListView()
{
    param(
        [Parameter(Mandatory=$true)][string]$SiteURL,
        [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential] $Cred,
        [Parameter(Mandatory=$true)][string]$ListName,
        [Parameter(Mandatory=$true)][string]$ViewName
    )

    Try {
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get the view to delete
        $View = $List.Views.getByTitle($ViewName)
    
        #Delete the list view
        $View.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
     }
    Catch {
        write-host -f Red "Error Deleting List View!" $_.Exception.Message
    }
}

#Call the function to delete list view
Remove-ListView -SiteURL "https://crescent.sharepoint.com" -Cred (Get-Credential) -ListName "Projects" -ViewName "Active Projects"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...