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
Error | Cause | Solution |
---|---|---|
Access Denied | Insufficient permissions | Ensure you have Edit rights on the list |
Cannot find list/view | Incorrect list/view name | Verify the list name using Get-PnPList |
View already exists | Duplicate view name | Use Get-PnPView before adding a new view |