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


Introduction

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.

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

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

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

  1. $siteURL = ""

  2. $userId = ""

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

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

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

  6. $ctx.credentials = $creds  

  7. try{      

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

  9. $views = $list.views      

  10. $ctx.load($views)      

  11. $ctx.executeQuery()      

  12. foreach($view in $views){          

  13. write-host $view.Title      

  14. }  }  

  15. catch{      

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

  17. }  

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.


Get View

The operations are similar as the above section. After getting the list views, get the particular view using GetByTitle method.

  1. $siteURL = ""

  2. $userId = ""

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

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

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

  6.  $ctx.credentials = $creds 

  7.  try{     

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

  9. $view = $list.views.GetByTitle("CustomView")      

  10. $ctx.load($view)      $ctx.executeQuery()      

  11. write-host $view.Title  }  

  12. catch{      

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

  14. }  

Next, we will see how we can create views on the site.


Create View

Here we will see how we can create a list view. The following steps depict the flow.

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

  • Initialize the ViewCreationInformation object.

  • Set the required parameters for new view. The necessary parameters are view name, type and view fields.

  • Add new view to the view collection. Then Load and execute the query.

  1. $siteURL = ""

  2. $userId = ""

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

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

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

  6. $ctx.credentials = $creds  

  7. try{      

  8. $list = $ctx.web.Lists.GetByTitle("TestList1")      

  9. $views = $list.views      

  10. $viewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation      

  11. $viewInfo.Title = "CustomView"     

  12. $viewInfo.ViewTypeKind = [Microsoft.SharePoint.Client.ViewType]::None      

  13. $viewInfo.ViewFields = @("ID", "Title", "Author")      

  14. $view = $views.Add($viewInfo)      

  15. $ctx.load($view)      

  16. $ctx.executeQuery()      

  17. write-host $view.Title  }  

  18. catch{      

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

The view will be added to the list. You will now see the view name changes. The following shows you the snapshot.

Next, we will see how to delete list views.


Delete List View Here we will see how we can delete the list view.

  • The following steps depict you the flow. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.

  • Get the list using GetByTitle method and then get views. From the view collection, find out the view to be deleted using GetByTitle method.

  • Then, remove the view using the delete object method.

  • Using the context, execute the query.

  1. $siteURL = ""

  2. $userId = ""

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

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

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

  6. $ctx.credentials = $creds  

  7. try{      

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

  9. $views = $list.views      

  10. $view = $views.GetByTitle("CustomView")      

  11. $view.DeleteObject()      

  12. $ctx.executeQuery()      

  13. Write-Host "View Deleted" }  

  14. catch{      

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

  16. }  

The view will be deleted from the list.


Update List View

Here we will see how we can update the list view.

  • The following steps depict you the flow.Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.

  • Get the list using GetByTitle method and then get views. From the view collection, find out the view to be updated using GetByTitle method.

  • In my case, I am trying to add a field to the view (Update Operation). You can do your own custom operation with your custom logic here.

  • Then, update the view using the update method.

  • Using the context, execute the query.

  1. $siteURL = ""

  2. $userId = ""

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

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

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

  6. $ctx.credentials = $creds  

  7. try{      

  8. $list = $ctx.web.Lists.GetByTitle("TestList1")      

  9. $views = $list.views      

  10. $view = $views.GetByTitle("CustomView")      

  11. $viewFields = $view.ViewFields      

  12. $viewFields.Add("Created")          

  13. $view.Update()      

  14. $ctx.executeQuery()      

  15. }  

  16. catch{      

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

The view will be updated.

Summary Thus you have learnt how to create, read, update or delete list views on SharePoint Online or O365 sites using CSOM with PowerShell commands.

Recent Posts

See All