Do you need to format the columns in your document library to highlight fields of a particular status or within a date range? This blog includes the example code the video demo on my YouTube channel.
To access the column formatting feature, go to your SharePoint List or Library and then click the heading of the column you want to format. You will bee the standard options including conditional formatting. The Advanced option gives access to the JSON formatting options.
Status field formatting example code
In this example the column is a choice field with the valuesDraft, Review, Approved. To add additional or different options update the code below with the new values.
{ "elmType": "div", "attributes": { "class": "=if(@currentField == 'Approved', 'sp-field-severity--good', if(@currentField == 'Draft', 'sp-field-severity--low', if(@currentField == 'Archived', 'sp-field-severity--warning', if(@currentField == 'Has issues', 'sp-field-severity--severeWarning', 'sp-field-severity--blocked')))) + ' ms-fontColor-neutralSecondary'" }, "children": [ { "elmType": "span", "style": { "display": "inline-block", "padding": "0 4px" }, "attributes": { "iconName": "=if(@currentField == 'Approved', 'CheckMark', if(@currentField == 'Draft', 'Forward', if(@currentField == 'Archvied', 'Error', if(@currentField == 'Has issues', 'Warning', 'ErrorBadge'))))" } }, { "elmType": "span", "txtContent": "@currentField" } ] }
Date range formatting example code
In this example, a date column is highlighted in red if the date is less than 30 days from now. This is useful for highlighting upcoming review dates.
{ "elmType": "div", "txtContent": "@currentField", "style": { "background-color": "=if(@currentField >= @now + 2592000000 || @currentField <= @now - 62208000000,'white', '#ffa59b')" } }
Email link example code
This example formats a ‘people picker’ field, getting the email address of the person whose name is in the field and formatting a template email if the user click the small email icon added by the formatting.
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "elmType": "span", "style": { "padding-right": "8px" }, "txtContent": "@currentField.title" }, { "elmType": "a", "attributes": { "iconName": "Mail", "class": "sp-field-quickActions", "href": { "operator": "+", "operands": [ "mailto:", "@currentField.email", "?subject=Procedure Query&body=[enter query here]\r\n---\r\n", "\r\nProcedures Site: https://tenantname.sharepoint.com/sites/sitename" ] } } } ] }
Additional resources
Microsoft article on JSON formatting including example code
https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting
GitHub resources including lots of example code
https://github.com/pnp/sp-dev-list-formatting/tree/master/column-samples
Thank you for sharing ! If it adds value I’ve added it a list with every sample from github on my blog : https://www.collabmania.com/2017/12/03/sharepoint-online-column-formatting-options/
Great, thanks Jeff
What if it is a calculated date field? It seems to make it behave differently
I’ll take a look. I haven’t tried using a calculated field with this, but that’s a really good question. I’ll let you know what I find.