Create, Retrieve, Update Or Delete List Views Using CSOM with PowerShell On SharePoint Online

In this article, you will learn how we can retrieve, create, update or delete list views using CSOM with PowerShell. This is mainly focused on using PowerShell scripts for SharePoint online sites.


Get Views:

First we will see how we can get the existing views available on the SharePoint site. The steps followed here are very similar to the steps following CSOM or JSOM programming.

  1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.

  2. Then access the list using the context and then the views from the list, load the objects and execute the query.

  3. Loop through the result object and get the necessary view information.


$siteURL = ""

$userId = ""

$pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

$ctx.credentials = $creds  

try

{      

$list = $ctx.web.Lists.GetByTitle("TestList")      

$views = $list.views      

$ctx.load($views)      

$ctx.executeQuery()      

foreach($view in $views)

{          

write-host $view.Title      

}  }  

catch

{      

write-host "$($_.Exception.Message)" -foregroundcolor red  

 

This will get all the view names available for the list on the site. Next, we will see how we can get one particular list on the site.