SharePoint Online: Set Default View using PowerShell

Requirement: Change default view in SharePoint Online.

How to Change Default View in SharePoint Online?
To set a custom list view as default view in SharePoint Online,
  • Go to List Settings >> Scroll down to the bottom and Click on the view name you wish to set as default view under "Views" section
  • In the Edit view page, Check the box next to "Make this the default view" and Click OK to save your changes.
    change default view in sharepoint online
So next time, when you hit: https://crescenttech.sharepoint.com/Lists/Projects/, you'll be taken in to the default view "Active Projects" at: https://crescenttech.sharepoint.com/Lists/Projects/Active%20Projects.aspx

Tips: In Modern Experience, We can use "Set Current View as Default" in Views Drop-down!

SharePoint Online: Set Default View using PowerShell
Here is the PowerShell script to change 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://crescenttech.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:
#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

    #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 -Credentials $Cred

#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.
SharePoint Online: Set Default View using PowerShell SharePoint Online: Set Default View using PowerShell Reviewed by Salaudeen Rajack on February 07, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.