SharePoint Modern UI: View Formatting

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.

  1. Title – Product Name
  2. Description – Product Description
  3. Price – Product Price
  4. Image – Product Picture
  5. InStock – Product Availability (true or false)

We will change the view of the list as shown in below screenshots.

Before Formatting

items

After Formatting

items

 

Pre-Requisites

  1. Modern UI
  2. Manage List and Views Permission
  3. 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-

containers

 

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-

  1. Element Type (elmType)
  2. Content (txtContent)
  3. CSS (style)
  4. Attributes (attributes)
  5. 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-

responsive

 

Complete Code

Complete JSON object can be downloaded from below link.

https://github.com/DanishIslam07/POCs/blob/master/products.json

References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s