SharePoint Online: Apply JSON View formatting using CLI for Microsoft 365

SharePoint Online: Apply JSON View formatting using CLI for Microsoft 365

SharePoint Online is a powerful platform for managing and sharing documents and other types of content within an organization. One of the key features of SharePoint Online is its ability to use JSON View formatting to customize the look and feel of lists and libraries.

However, applying JSON View formatting can be a time-consuming and repetitive task, especially if you need to apply the same formatting to multiple sites or lists. Fortunately, we have a command-line tool called CLI for Microsoft 365 that makes it easy to automate this process.

In my previous blogs we saw how to apply SharePoint online JSON view formatting using SharePoint REST API and PnP PowerShell. In this blog post, we will explore how to use CLI for Microsoft 365 to apply JSON View formatting to SharePoint Online lists.

The first step is to install CLI for Microsoft 365. You can do this by following the instructions on the official documentation page: CLI for Microsoft 365 – Installation

Then you can use below CLI for Microsoft 365 script to apply JSON View formatting to your SharePoint online list:

# Display name of SharePoint list
$listName = "Ganesh Sanap Blogs"

# Name of SharePoint list view
$viewName = "All Items"

# JSON to apply to view formatting
$jsonViewFormatting = @'
{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
    "additionalRowClass": "=if([$DueDate] <= @now, 'sp-field-severity--blocked', '')"
}
'@

# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

# Apply JSON view formatting
m365 spo list view set --webUrl $siteUrl --listTitle $listName --title $viewName --CustomFormatter $jsonViewFormatting

Where [$DueDate] is the internal name of your SharePoint column.

Once you run above script successfully and navigate to SharePoint list view, you will see that new JSON view formatting is applied to your SharePoint online list view using CLI for Microsoft 365:

SharePoint Online modern experience to apply JSON View formatting using CLI for Microsoft 365
SharePoint Online – Apply JSON View formatting using CLI for Microsoft 365

Conclusion

Using CLI for Microsoft 365, you can easily apply JSON View formatting to your SharePoint Online lists, saving time and reducing the risk of errors. By following the steps outlined in this blog post, you can quickly get started with using CLI for Microsoft 365 to apply JSON View formatting to your SharePoint Online lists.

Learn more