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:

  1. Login to your SharePoint Online site >> Navigate to the list and pick the target view you would like to format.
  2. In the Views dropdown, select “Format current View”.format list view in sharepoint online
  3. Apply the JSON to format the view. Preview the results and hit the “Save” button once finalized.alternate rows sharepoint online list view

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

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Format List View using PowerShell

  • 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?

    Reply
    • 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

      Reply

Leave a Reply

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