Customize SharePoint columns using column formatting

Hi Everyone,

You all might have heard about customized column formatting for SharePoint lists and libraries. Column formatting allows us to change how SharePoint columns inside lists and libraries look and is used mainly for the purpose of better visibility and look. Column formatting is applied on the basis of data column is holding without changing it. SharePoint by default provides column formatting, you might have seen below column formatting for choice field:

Figure 1: Default Column formatting for choice field

Today, we will customize two different columns in SharePoint list. For demonstration purpose, I have setup a list which shows information about the books. Two columns whose display we are going to change are of type number and choice. Before column formatting list looks like below:

Figure 2: Before column formatting

Let’s see how to do it!!

  1. Navigate to the SharePoint list.
  2. Click column header which you want to customize.
  3. Under Column settings, click Format this column.
Figure 3: Open column formatting pane for selected column

4. When the formatting panel is opened, click Advanced mode. As we are going to customize columns more, we will not use SharePoint provided column formatting.

Figure 4: Open Advanced mode

5. In the editor, paste following code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "span",
      "txtContent": "@currentField",
      "style": {
        "padding-left": "10px",
        "white-space": "nowrap"
      }
    }
  ],
  "attributes": {
    "class": "sp-field-dataBars"
  },
  "style": {
    "padding": "0",
    "width": "=if(@currentField == [$No_x002e_OfCopies], '100%', (@currentField * 100)/ [$No_x002e_OfCopies] + '%')"
  }
}

6. To describe what is there in the JSON in short:
a. txtContent is the Property to show data which is set to current field data (@currentField).
b. We have mentioned formatting of type data bars in attributes -> class property.
c. Data bar width is conditional, we are comparing values from two columns and then setting width of data bar.
7. After formatting number column, list looks like below:

Figure 5: After number column formatting

8. If you want number column in percentage format, just change

"txtContent": "@currentField"

to

"txtContent": "=(100 * @currentField)/[$No_x002e_OfCopies] + ' %'"

(No_x002e_OfCopies is nothing but Total No. Of Copies column). Check the below output in percentage format.

Figure 6: After number column formatting to display bar data in percentage


9. Similary, we will change for choice column. Based on the status value, it displays background-color and status icon. Check the following JSON code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      },
      "attributes": {
        "iconName": "=if(@currentField == 'Ordered','Error', if(@currentField == 'Available', 'Accept', if(@currentField == 'Issued', 'ErrorBadge', '')))"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ],
  "attributes": {
    "class": "=if(@currentField == 'Ordered','sp-field-severity--warning', if(@currentField == 'Available', 'sp-field-severity--good', if(@currentField == 'Issued', 'sp-field-severity--severeWarning','')))"
  }
}

10. After choice column formatting, list looks like below:

Figure 7: After choice column formatting

11. In the choice column formatting, we have used three different icons and background-colors based on its choice values.
12. You just have to change the JSON code as per your column’s internal names and values.
13. Column formatting is easy and fastest way to have better look & feel of list.

References:
https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting

You may also like...

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

%d bloggers like this: