SharePoint Online: Set Default View using PowerShell
Requirement: Change the default view in SharePoint Online.
How to Change Default View in SharePoint Online?
Want to make a specific view the default view for all users? In many cases, you’ll want to make a specific view the default view for lists or libraries in SharePoint Online. In this post, we’ll walk through the steps necessary to set a default view for a list or document library.
To set a custom list view as the default view in SharePoint Online, do the following steps:
- Go to List Settings >> Scroll down to the bottom and click on the view name you wish to set as the default view under the “Views” section.
- In the Edit view page, check the box next to “Make this the default view” and click OK to save your changes.
So next time, when you hit: https://Crescent.sharepoint.com/Lists/Projects/, you’ll be taken into the default view “Active Projects” at: https://Crescent.sharepoint.com/Lists/Projects/Active%20Projects.aspx
Similarly, for Modern lists and libraries, You can make a view default from the views dropdown:
SharePoint Online: Set Default View using PowerShell
Here is the PowerShell script to change the default 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"
Function Set-SPODefaultView()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ViewName
)
Try {
#Setup 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 list view by title
$View=$List.Views.GetByTitle($ViewName)
#Set the view as default view
$View.DefaultView=$True
$View.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "List View '$ViewName' is Set as the Default View!"
}
Catch {
write-host -f Red "Error Setting Default View!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Projects"
$ViewName="Active Projects"
#Call the function
Set-SPODefaultView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName
We also have MobileView, MobileDefaultView properties to enable mobile view and make a default view for mobile!
PnP PowerShell to Set a List View as Default View:
Here is how to make a view default in SharePoint Online:
#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 Context
$Context = Get-PnPContext
#Get the List View from the list
$ListView = Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop
#Set View as Default View
$ListView.DefaultView = $True
$ListView.Update()
$Context.ExecuteQuery()
Write-host -f Green "View '$ViewName' Set as Default View of the List!"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
You can also use: Set-PnPView to set a view as default:
Connect-PnPOnline -Url $SiteURL -Interactive
#Set the List View as Default view
Set-PnPView -List $ListName -Identity $ViewName -Values @{DefaultView=$True}
With Set-PnPView cmdlet, you can update properties of a list view such as: Title, Hidden, JSLink, RowLimit, ViewQuery, Etc.