Managing SharePoint List Views using PnP PowerShell

Loading

In SharePoint Online, list views help users organize and display data efficiently. Using PnP PowerShell, you can create, update, delete, and manage views in a SharePoint list. This is useful for automation, governance, and user experience optimization.


What You’ll Learn:

✔️ How to connect to SharePoint Online
✔️ How to create a new list view
✔️ How to modify an existing list view
✔️ How to delete a list view
✔️ How to retrieve and export list views


Prerequisites

Before proceeding, ensure that:
PnP PowerShell is installed
You have SharePoint Online permissions
You know the list name and desired view settings


Step 1: Install and Import PnP PowerShell

If you haven’t installed PnP PowerShell, install it using:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint Online

Before managing list views, connect to your SharePoint site:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your tenant name
🔹 Replace "yoursite" with the actual site name

Connected successfully!


Step 3: Retrieve All Views from a SharePoint List

To get all views from a specific list, use:

# Define list name
$listName = "Project Tasks"

# Get all views
$views = Get-PnPView -List $listName

# Display views
$views | ForEach-Object { Write-Host "View: " $_.Title }

All views retrieved!


Step 4: Create a New List View

To create a new view in a SharePoint list:

# Define variables
$listName = "Project Tasks"
$viewName = "Active Tasks"
$fields = @("Title", "Status", "DueDate")

# Create a view
Add-PnPView -List $listName -Title $viewName -Fields $fields -ViewType Html -Paged

Write-Host "View '$viewName' created successfully!"

🔹 Options:

  • -ViewType: "Html", "Calendar", "Grid", "Chart"
  • -Paged: Enables pagination

New list view created!


Step 5: Update an Existing List View

To modify an existing list view (e.g., add/remove columns):

# Define variables
$listName = "Project Tasks"
$viewName = "Active Tasks"
$newFields = @("Title", "Status", "AssignedTo", "DueDate")

# Update the view
Set-PnPView -List $listName -Identity $viewName -Fields $newFields

Write-Host "View '$viewName' updated successfully!"

List view updated!


Step 6: Delete a List View

To remove an existing view:

# Define variables
$listName = "Project Tasks"
$viewName = "Old View"

# Delete the view
Remove-PnPView -List $listName -Identity $viewName -Force

Write-Host "View '$viewName' deleted successfully!"

List view deleted!


Step 7: Export List Views to a CSV File

To export all views and their details:

# Define list name
$listName = "Project Tasks"

# Get all views
$views = Get-PnPView -List $listName

# Export to CSV
$views | Select Title, DefaultView, ViewQuery | Export-Csv -Path "C:\ListViews.csv" -NoTypeInformation

Write-Host "List views exported to C:\ListViews.csv"

List views exported!


Common Errors & Solutions

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you have Edit rights on the list
Cannot find list/viewIncorrect list/view nameVerify the list name using Get-PnPList
View already existsDuplicate view nameUse Get-PnPView before adding a new view

Leave a Reply

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