Similar to column formatting, Microsoft Lists allows you to tailor list views to your own business needs via custom formatting. This way, you can create unique business scenarios that can be filtered automatically with the click of a button.
In this chapter, you will learn how to take your lists to the next level by covering the following main topics:
You can find the code files present in this chapter on GitHub at https://github.com/PacktPublishing/Hands-On-Microsoft-Lists/tree/main/Chapter07.
With Microsoft Lists, you can take customizations to the next level and format the entire view instead of just the columns.
View formatting supports the same formatting methods we saw in the Column formatting section of this book; that is, WYSIWYG and JSON formatting.
In this section, we will explain how to format a view in Microsoft Lists without having to explain the HTML and JSON syntax. If you need help remembering the options that are available for formatting, please check out the Column formatting section of this book.
Before we start exploring the more technical areas of view formatting, you must know how to format a view.
To gain access to the customization interface, do the following:
Like column formatting, view formatting also includes a WYSIWYG design mode that allows you to format your view and see your changes in real time.
The following table shows the available formatting options for the default list layouts:
The alternating row styles allow you to define a background color and text styles for the odd rows so that they are different to the even rows. To format your view with alternating row styles, do the following:
Conditional formatting allows you to define conditions so that you can apply background colors and text formatting to your rows. To format your view with conditional formatting, do the following:
The Card Designer allows you to modify the card that's applied to the Gallery view. To select which columns must appear in the card, do the following:
JSON formatting allows you to customize views beyond what the design mode and the WYSIWYG interface allow you to do.
Before we start digging into the examples and into the more complex customizations, you must become familiar with the JSON properties that are specific to Microsoft Lists views, combined with the HTML and JSON properties we saw in the Column formatting section so that you can build list view custom layouts.
As a combination of HTML and JSON, view formatting has its own properties that are used along with the HTML. The following table shows the specific properties you should use when formatting views:
When formatting a view, you can take advantage of the pre-existing CSS styles provided by Microsoft default classes. The following table shows the specific CSS classes that can be applied to the view formatting. These can then be complemented by the CSS classes we saw in the Column formatting section:
Microsoft Lists view formatting makes use of special strings that will give you access to direct properties and values in the context of the view you are formatting. The following table shows all the available special strings. All the custom strings we saw in the Column formatting section can be used for view formatting:
Microsoft Lists advanced formatting includes the Monaco source code editor. By default, it will highlight your code, identify syntax errors in the JSON, and validate whether the properties you are using are allowed by the formatting schema.
Despite providing these features, there is one thing that the Monaco editor does not provide: the ability to save the formatting locally in a file. To avoid losing your customizations, we recommend that you use a source code editor such as Visual Studio Code. To get started, follow these steps:
Once you have written the code, you need to add it to your list column by doing the following:
Important
If you decide to use the Monaco editor exclusively, make sure you save a copy of your customizations. Clicking the Switch to design mode option will make any custom code disappear.
Now that you have looked at the Microsoft List column formatting and view formatting schemas, it's time to start learning how to build your own JSON. There is nothing better than learning from real examples that are described and then exemplified.
Quick Tip
In the Microsoft 365 Patterns and Practices (PnP) open source repository, you can find numerous templates that you can use in your own lists or use as a starting point to build your own view formatting.
The column samples repository is available at https://github.com/pnp/sp-dev-list-formatting/tree/master/view-samples.
One of the simplest view formattings you can do is create a custom alternate row formatting. The following code formats the even rows with a background color.
This example can also be achieved in design mode. However, by using JSON, you can customize it even further:
{
"$schema": "https://developer.microsoft.com/json- schemas/sp/view-formatting.schema.json",
"additionalRowClass": "=if(@rowIndex%2==0,'ms-bgColor- magenta ms-fontColor-black ms-fontColor-white--hover ms- bgColor-redDark--hover','')"
}
The following screenshot shows the result of applying the code to the view:
In this example, you can observe the following:
Tile layout formatting allows you to format each column row as a tile, which allows you to use any HTML structure to display the content.
John has a Microsoft List connected to Microsoft Power Automate that's being used to store the daily forecast for the company's office locations. Inspired by the Microsoft Windows weather tiles, John has decided to format his list to display the weather information for each of the cities in tile format:
The following code snippet will transform the weather data stored in the list into a tile view:
{
"$schema": "https://developer.microsoft.com/json- schemas/sp/view-formatting.schema.json",
"hideSelection": true,
"width": "250",
"height": "250",
"formatter": {
"elmType": "div",
"children": [{
"elmType": "div",
"style": {
"display": "inline-block",
"min-width": "250px",
"min-height": "250px",
"margin-right": "10px",
"box-shadow": "2px 2px 4px darkgrey"
},
…
https://github.com/PacktPublishing/Hands-On-Microsoft-Lists/blob/main/Chapter07/weather.json
In this example, you can observe the following:
This example shows how flexible the tile layout can be and how it can be used as a starting point for other scenarios.
List views can be grouped and aggregated by the columns containing all the information that have the same value aggregate inside an accordion.
With view formatting, you can go a step further and also customize the headers and the footers of your groups, as shown in the following screenshot:
In this example, you can observe the following:
{
"$schema": "https://developer.microsoft.com/json- schemas/sp/v2/row-formatting.schema.json",
"groupProps": {
"headerFormatter": {
"elmType": "div",
"style": {
"font-weight": "800"
},
"txtContent": "= @group.count + if(@group.count > '1', ' computers', ' computer') + ' with ' + @group.fieldData.displayValue + ' in stock'"
},
"footerFormatter": {
"elmType": "div",
"style": {
"font-weight": "800"
},
"txtContent": "= 'Total value: ' + @columnAggregate.value"
}
}
}
https://github.com/PacktPublishing/Hands-On-Microsoft-Lists/blob/main/Chapter07/groups.json
With that, we have learned how to format lists using column formatting and view formatting. Use the examples provided to get started and then adjust them to your own needs.
In this chapter, you learned how to customize a list view using the built-in tools and using a combination of JSON and HTML.
The JSON schema, which we covered in the column formatting and view formatting chapters of this book, is the foundation for making code customizations in Microsoft Lists. As we'll see in the next chapter, the same syntax can be reused to customize list forms.
3.138.122.4