SharePoint Online: PowerShell to Update List View

Requirement: Update List View in SharePoint Online using PowerShell.

PowerShell to Update  SharePoint Online List View

SharePoint Online: PowerShell to Update List View

This blog post will show you how to use PowerShell to update a list view. We will walk you through the process of updating the filter settings for a list view, sorting order, and updating list view columns. This can be a helpful tool if you need to make changes to a list view on multiple sites or if you want to script the updates.

Let’s apply a filter to the “Active Projects” view to get all projects with the “Project Status” field value as “Active.”

#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 to Update List View in SharePoint Online
Function Update-SPOListView($SiteURL, $ListName, $ViewName, $ViewQuery)
{
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Get the view to update
        $View = $List.Views.GetByTitle($ViewName)
        $Ctx.ExecuteQuery()
 
        If($View -ne $NULL)  
        {
            #Update the View Query
            $View.ViewQuery = $ViewQuery
            $View.Update()
            $Ctx.ExecuteQuery()
 
            Write-host "View Updated Successfully!" -f Green
        }
        else
        {
            Write-host "View '$ViewName' doesn't exist in the List!"  -f Yellow
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ViewName="Active Projects"
$ViewQuery ="<Where><Eq><FieldRef Name = 'ProjectStatus' /><Value Type ='Choice'>Active</Value></Eq></Where>"

#Call the function
Update-SPOListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -ViewQuery $ViewQuery 

Update View in SharePoint Online using PnP PowerShell

Let’s update a list view by setting a filter to it, using PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ViewName="Active Projects"
$ViewQuery ="<Where><Eq><FieldRef Name = 'ProjectStatus' /><Value Type ='Choice'>Active</Value></Eq></Where>"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Client Context
$Context = Get-PnPContext
 
#Get the List View
$View = Get-PnPView -Identity $ViewName -List $ListName
 
#Update the view Query
$View.ViewQuery = $ViewQuery
$View.Update() 
$Context.ExecuteQuery() 

PnP PowerShell to Set View Type to “Tiles” in SharePoint Online

Here is another example of updating view settings in SharePoint Online.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"
$ViewName= "All Documents" 

#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Interactive

#Set View Properties
Set-PnPView -List $ListName -Identity $ViewName -Values @{"ViewType2" = "TILES"} 

What are the other properties that can be updated through PowerShell?

  1. Aggregations
  2. AggregationsStatus
  3. ColumnWidth
  4. ContentTypeId
  5. CustomFormatter
  6. DefaultView
  7. DefaultViewForContentType
  8. EditorModified
  9. Formats
  10. Hidden
  11. IncludeRootFolder
  12. JSLink
  13. ListViewXml
  14. Method
  15. MobileDefaultView
  16. MobileView
  17. NewDocumentTemplates
  18. ObjectVersion
  19. Paged
  20. RowLimit
  21. Scope
  22. TabularView
  23. Tag
  24. Title
  25. Toolbar
  26. ViewData
  27. ViewJoins
  28. ViewProjectedFields
  29. ViewQuery
  30. VisualizationInfo

Related posts:

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: PowerShell to Update List View

  • Good day!

    Thank you for the explanation of query language. Unfortunatly, the following code is not working for me. This drives me crazy.

    $ViewQuery =””

    Any idea what I’m doing wrong?

    Kind regards

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *