SharePoint Online: Format List View using PowerShell
Requirement: Apply formatting to SharePoint Online View using PowerShell.
How to Apply Formatting to a List View in SharePoint Online?
Similar to column formatting, we can apply formatting to SharePoint Online views as well. Say, you want to apply alternate rows highlighting format for a SharePoint Online list view. Here are the steps to apply formatting to a list view:
- Login to your SharePoint Online site >> Navigate to the list and pick the target view you would like to format.
- In the Views dropdown, select “Format current View”.
- Apply the JSON to format the view. Preview the results and hit the “Save” button once finalized.
PowerShell to Set List View Formatting
Alright, How about setting the view format using PowerShell? Here is the PowerShell script to apply formatting to a SharePoint list view:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ViewName = "Active Projects"
$JSONFormat = @"
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
"additionalRowClass": "=if(@rowIndex % 2 == 0,'ms-bgColor-themeLighter ms-bgColor-themeLight--hover','')"
}
"@
Try
{
#Connect to site
Connect-PnPOnline -URL $SiteURL -Interactive
#Set the View Formatting
Set-PnPView -List $ListName -Identity $ViewName -Values @{CustomFormatter =$JSONFormat} -ErrorAction Stop
Write-host "View Format Applied Successfully!" -foregroundcolor Green
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Use this GitHub resource for list view formats from the community: https://github.com/pnp/List-Formatting/tree/master/view-samples