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
SharePoint Online: Format List View using PowerShell – Is this possible to achieve using SPO CSOM, not PnP? If yes, could you please show/share the required code?
per this documentation https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.view.customformatter?view=sharepoint-csom
the View object suppose to have a CustomFormatter attribute. But when I try to add anything it says the property doesn’t exist on that object.
3 thoughts:
– the CustomFormatter is a RemoteAttribute, so it perhaps needs to be loaded differently?
– maybe would be useful to have a look on the PnP source code, on how it is done. I believe they use CSOM in the background
– it should be also possible to use Graph API