Easy lists and libraries customizations

Easy lists and libraries customizations

Column and view formatting is a way to edit how lists and libraries displays their data in SharePoint Online. This feature does not change the data, it only affect how the data is visualy rendered.

Another option would be creating a SharePoint Framework field customizer solution. This second option is more powerful because you can write any code to control how the field is rendered. It also require more JavaScript/TypeScript coding skills

We call column and view formatting a “no code” solution, which is true in a sense, but you will still have to give it some
In this article, you will information about how this feature works, some examples and a community based resource of exemples that you can copy/paste in your environment.(no coding)

So let’s first talk about view formatting :

To give you some visibility, we will start with an example where you want to highlight a list row, based on the value of a status column. In our case we have some items and a status column containing the values : “Not Started”, “Blocked” and “Completed”.


SharePoint column status

In order to format our view, we will click on the view, then format current view :

View formatting menu

Once you do so, you will see a configuration panel with some information, a link to documentation and a text editor.
We will first highlight all row in this list, so we provide the following Json :

{
  "schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "additionalRowClass": "sp-field-severity--good"
}

And press preview :

View formating simple

Yay ! everything is green !

So in this Json code, we first provided the view formatting schema (You never change this for all your views formatting). And provided an attribute “additionalRowClass”: “sp-field-severity–good”. It means we want to apply this class on all rows.
You can find the supported classes Here

To go a little further, we will now apply a conditional class to format our view

{
  "schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "additionalRowClass": "=if([$Status] == 'Done', 'sp-field-severity--good', if([$Status] == 'Not started', 'sp-field-severity--low' , if([$Status] == 'Blocked','sp-field-severity--blocked', '')))"
}


This time, we are providing an if statement for the “additionalRowClass” attribute. In order to base the condition on the Status field, we use
if([$Status] == ‘the value of the status column’, ‘the class to apply if it is the case’, ‘the class to apply otherwise’.
And we nest other if statements in ‘the value otherwise’.

This code might be overwhelming at first, but you know, I didn’t write it by myself :). I found it in the community based resouce on github, you can find it there. I just changed the ‘Not started’ value and deleted ‘In review’ to fit my example.
Here is the result :

View formatting conditional

Now let’s have a look at Column formatting

Column formatting is pretty similar, but in this case we want to change the display of a column value. Some handy use cases would be where you want to highlight a row based on a value, display pictures, add an “email link” in a people column or even trigger a flow <3. It is the right moment to give you a name... Chris Kent.
Yes, using these feature will greatly improve the SharePoint Online experience. It will make you peers move from Excel to SharePoint Online lists. You will be called the new Super Man

Superman

But this guy actually exists, and he has provided amazing samples, video and visibility to these features. I strongly recommend you visit his website Here

So let’s take an example, similar to the previous one.
We will format a column based on its value, we will provide a class and also an icon from the Office ui fabric.

So let’s take a document library this time, and create a Status choice column with the values “Not Started”, “Blocked” and “Completed” :

Document library status

In order to format our column, we will click on the Status column, column settings, format this column :

Column formatting menu

In this case, Microsoft provide you with a template where you can change the color of that column based on a value. This template is context aware, it will provide you with different options based on the column type. In the future we will see more of those templates 🙂
To customize our column we will click on advance mode :

Clumn formatting avanced mode

At this point, we will paste the following Json code :

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
    "elmType": "div",
    "attributes": {
        "class": {
            "operator": "+",
            "operands": [
                {
                    "operator": "?",
                    "operands": [
                        {
                            "operator": "==",
                            "operands": [
                                "@currentField",
                                "Completed"
                            ]
                        },
                        "sp-field-severity--good",
                        {
                            "operator": "?",
                            "operands": [
                                {
                                    "operator": "==",
                                    "operands": [
                                        "@currentField",
                                        "Not Started"
                                    ]
                                },
                                "sp-field-severity--low",
                                {
                                    "operator": "?",
                                    "operands": [
                                        {
                                            "operator": "==",
                                            "operands": [
                                                "@currentField",
                                                "Blocked"
                                            ]
                                        },
                                        "sp-field-severity--severeWarning",
                                        "sp-field-severity--blocked"
                                    ]
                                }
                            ]
                        }
                    ]
                },
                " ms-fontColor-neutralSecondary"
            ]
        }
    },
    "children": [
        {
            "elmType": "span",
            "style": {
                "display": "inline-block",
                "padding": "0 4px"
            },
            "attributes": {
                "iconName": {
                    "operator": "?",
                    "operands": [
                        {
                            "operator": "==",
                            "operands": [
                                "@currentField",
                                "Completed"
                            ]
                        },
                        "CheckMark",
                        {
                            "operator": "?",
                            "operands": [
                                {
                                    "operator": "==",
                                    "operands": [
                                        "@currentField",
                                        "Not started"
                                    ]
                                },
                                "Forward",
                                {
                                    "operator": "?",
                                    "operands": [
                                        {
                                            "operator": "==",
                                            "operands": [
                                                "@currentField",
                                                "Blocked"
                                            ]
                                        },
                                        "Warning",
                                        "ErrorBadge"
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        },
        {
            "elmType": "span",
            "txtContent": "@currentField"
        }
    ]
}
omg


Ok, this one is quite big 🙂 This is the way to write the customization for SharePoint Online and On Premise (AST).

In this example you can see that we want to render our column as a Div (“elmType”: “div” at line 3), then we give it an attribute of type class (“attributes”: { “class”: { at line 4/5) and provide Operators and Operands (this represent our if statement). This is actually JavaScript operators (? for if and == for equal). This all part correspond to the class we provide to this column to render a color based on the Status value. Exactly like the View formatting example, but written another way.
After this pretty big if statement, we provide another attribute which is “Children” at line 52. This part represent the icon we want to display. Here we provide an icon name that you can find in the Office ui fabric icons.
An finally we would like the value of the field to be displayed, so we provide another attribute {“elmType”: “span”, “txtContent”: “@currentField” } at line 103/104. This will diplay the current field value.

Here is the result :

Column formatting conditional

You can find the sample Here

This example did show you how to format column in SharePoint Online and On Premise, you can also do the same customization with the short code below only in SharePoint Online :

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
    "elmType": "div",
    "attributes": {
      "class": "=if(@currentField == 'Completed', 'sp-field-severity--good', if(@currentField == 'Not Started', 'sp-field-severity--low', if(@currentField == 'Blocked', 'sp-field-severity--blocked', 'sp-field-severity--blocked'))) + ' ms-fontColor-neutralSecondary'"
    },
    "children": [
      {
        "elmType": "span",
        "style": {
          "display": "inline-block",
          "padding": "0 4px"
        },
        "attributes": {
          "iconName": "=if(@currentField == 'Completed', 'CheckMark', if(@currentField == 'Not Started', 'Forward', if(@currentField == 'Blocked', 'Error', 'ErrorBadge')))"
        }
      },
      {
        "elmType": "span",
        "txtContent": "@currentField"
      }
    ]
  }

The Community based sample ressource

All community samples will contain a readme file, some png for illustration and a .json file where you fint the code to copy/paste. In some cases you will see two .json file, one for SharePoint Online and one (AST) for SharePoint On Premise and Online. This is because, as you can appreciate from the last example, SharePoint Online allow easier typing.

You can find all the sample for View and Column Formatting at this address and choose between view sample and column sample

I found a really good video about this sample resource :

Here is another video to get started with column formatting :

And a last video on how to use the community based ressource :


Their is also a plugin for google chrome, I found a very nice article talking about it here


Bonus : Use column formatting to launch a flow

As a bonnus to this article, I would like to show you another example which is very interesting. This example has been shown in this video by Chris Kent :

You can find the Json code to paste in your document library or list Here. I have been using this on in a document library to trigger an approval flow 😉

I hope you found this article interesting, you can check some related content under the SharePoint Online Workshop

See you soon in the blog section !

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.