SharePoint Online: Get List Views using PowerShell
Requirement: Get SharePoint Online List Views using PowerShell.
PowerShell to Get All Views from a List:
This blog post will show you how to get all list views from your SharePoint Online list 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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#$Ctx.Load($List)
#Get All List Views of the List
$ctx.Load($List.Views)
$Ctx.ExecuteQuery()
Write-host "Total Views in the List:"$List.Views.Count
#Iterate through each View
ForEach($View in $List.Views)
{
#Get the View
$ctx.Load($View)
$Ctx.ExecuteQuery()
Write-Host -f Yellow $View.Title
}
}
Catch {
write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
Get the Default View of the List:
#Get the Default View of the List
$DefaultView = $List.DefaultView
$Ctx.Load($DefaultView)
$Ctx.ExecuteQuery()
Write-Host $DefaultView.Title
Get a Specific View from the List
This PowerShell script gets a specific view from a SharePoint Online list or library:
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$ViewTitle ="All Items"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#Get the List View
$ListView = $List.views.GetByTitle($ViewTitle)
$Ctx.load($ListView)
$Ctx.executeQuery()
#Get the View URL
Write-host "URL of the List View:"$ListView.ServerRelativeUrl
}
Catch {
write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
Get All Views from a List using PnP PowerShell
We can get a list of available views for a specific list by utilizing the PnP PowerShell cmdlet Get-PnPView:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All List Views from the list
$ListViews = Get-PnPView -List $ListName
#Iterate through each View
ForEach($View in $ListViews)
{
Write-host $View.Title
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Similarly, to get a specific view using PnP PowerShell, use:
#Config Variables
$SiteURL = "https://Crescent.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
#Get the List View from the list
$ListView = Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop
#Get All Properties of the View
$ListView | Select-Object -Property *
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
You have saved me an immense amount of time by having such a fantastic site.
You do a much better job of training than Microsoft does of their own products!
Thank you thank you thank you!