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 containing the view you want to delete.
  • 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
Please note, Except the default view, You can delete any existing views of the SharePoint Online lists and libraries. If you want to delete a default view, you must first set a different view as the default, and then delete the former default view!

SharePoint Online: Delete Custom View using PowerShell
Here is the PowerShell to Delete a view in SharePoint Online
#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"

SharePoint Online: Delete a view using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Remove List View
    Remove-PnPView -List $ListName -Identity $ViewName -Force -ErrorAction Stop
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: Delete List View using PowerShell SharePoint Online: Delete List View using PowerShell Reviewed by Salaudeen Rajack on August 18, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.