In this post we will explore View Formatting for List/Library, announced sometime back by Microsoft. It allows to change the display of view by formatting the rows. Different columns (or) values constituting a row are defined by different html elements and style is applied to every elements. These html elements are defined as JSON object. View formatting only changes the ways rows/items are displayed and there is no impact on data.
We have a Products list, with columns as described below.
- Title – Product Name
- Description – Product Description
- Price – Product Price
- Image – Product Picture
- InStock – Product Availability (true or false)
We will change the view of the list as shown in below screenshots.
Before Formatting
After Formatting
Pre-Requisites
- Modern UI
- Manage List and Views Permission
- Visual Studio Code (for easier syntax formatting and suggestions)
How does this works ?
First of all we need to identity the layout of content i.e. where to place different values of row. In my case, i have divided the data to be displayed as containers as per the below layout-
Now i have to create JSON object based on this layout. Basic object with parent container looks like below-
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json", "hideSelection": true, "hideColumnHeader": true, "rowFormatter": { "elmType": "div", "attributes": { "class": "ms-borderColor-neutralLight" }, "style": { "display": "flex", "flex-wrap": "wrap", "align-items": "stretch", "padding": "8px", "margin-bottom": "16px", "max-width": "930px", "border-radius": "8px", "box-shadow": "4px 4px 8px darkgrey" }, "children": [ ] } }
So lets understand above object first. First line defines the schema location which exposes four properties for View Formatting-
- rowFormatter – it defines the format of the list view row. Its schema can be checked here
- additionalRowClass – it specifies the styling for the row
- hideSelection – provides ability to disable or enable row selection. Default is false.
- hideColumnHeader – provides the ability to show or hide column headers. Default is false.
Note:- “rowFormatter” and “additionalRowClass” are mutually exclusive properties. Anything specified in “additionalRowClass” will be over-ridden by “rowFormatter” (if used)
We are using above defined 3 properties in our JSON object (as shown in code). An element is defined by mainly 4 properties-
- Element Type (elmType)
- Content (txtContent)
- CSS (style)
- Attributes (attributes)
- Child Elements (children)
Using the above element architecture, we can describe the entire layout for a row and thus create the JSON object. Once the object is ready, navigate to site -> List/Library and click on “Format Current View” and provide the JSON in “Format View” window.
Click “Preview” button to preview the changes. if everything is Ok, click “Save” button.
This formatting is also responsive as can be seen in below screenshot-
Complete Code
Complete JSON object can be downloaded from below link.
https://github.com/DanishIslam07/POCs/blob/master/products.json
References
- https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/view-formatting
- https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting
- https://github.com/SharePoint/sp-dev-list-formatting/tree/master/view-samples
- https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json
- https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json#definitions/elm
Hi there….you can connect with me via Contact page.
LikeLike
Would you be concerned with exchanging hyperlinks?
LikeLike
Nope.
LikeLike
With SharePoint dynamically building classes, is there anyway to use a wildcard to overcome this?
{
“$schema”: “https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json”,
“hideSelection”: false,
“hideColumnHeader”: false,
“rowFormatter”: {
“elmType”: “div”,
“attributes”: {
“class*”: “cellTitle-”
LikeLike