Auto-Set Current Date in SharePoint with JSON Formatting
Image Source: Shutterstock.com
Jan 16, 2024 9:00 PM

Auto-Set Current Date in SharePoint with JSON Formatting

by HubSite 365 about Michel Mendes [MVP]

Microsoft MVP | Senior Consultant at Avanade | Microsoft 365 | Power Platform | SharePoint

External Blog Post
Pro User

Learning Selection

Automate SharePoint Date Fields with JSON List Formatting!

Key insights

 

In a recent query from a reader on List Formatting and setting field values on Microsoft Lists, a reader sought help to automatically populate a date column with the current date when an action button is clicked. This is intended to simplify the process for executives to confirm the completion of a work trip.

It is possible to set a SharePoint Date only field to the current date using JSON List Formatting. The customRowAction attribute coupled with setValue action type can be utilized for this task. The @now token is used to retrieve the current date/time automatically and can be formatted to show the date only by generating a string in the 'yyyy-MM-dd' format.

A code sample demonstrates how to create a custom button that, on being clicked, sets a Date only field named 'Date Trip Completed' to the current date. If a date has already been set, it displays the date without showing the button to mark the trip as complete. The JSON provided can be applied with just a change to the field name, if different.

The result of applying this formatting template is a functional button that sets the date field's value to the current one upon clicking. However, the process and technical specifics of the implementation are not displayed in this summary.

The post concludes with references to the Formatting Syntax Reference on Microsoft Learn, where readers can find further information on List Formatting in SharePoint.

 

Understanding SharePoint List Formatting with JSON

SharePoint List Formatting allows users to customize the appearance and behavior of list data using JSON. This enables users to add dynamic elements like action buttons to lists, enhancing interactivity and user experience. With JSON List Formatting, users can display data in various ways, set conditions for formatting, and trigger actions such as setting field values. In professional settings, such as the user's intention in our example, this can be especially useful for streamlining processes and creating a user-friendly interface that fits into workflows, such as confirming the completion of work travel. It requires a solid understanding of both SharePoint and JSON syntax but provides a powerful toolset for tailoring list views to meet specific business needs.

 

In a recent query highlighted on a blog, a reader inquired about adding an action button to a Microsoft list that automatically sets a trip date column to the current date when pressed. This feature would help executives easily confirm the completion of their work trips. Questions arose whether this could be accomplished using SharePoint List Formatting with JSON coding.

The solution involves utilizing the 'customRowAction' attribute with the 'setValue' action type in SharePoint List Formatting. This technique sets the field value to the current date by using the unique '@now' token that automatically identifies the current date and time.

To exclusively set the date field, one can construct a string formatted as 'yyyy-MM-dd'. The following example demonstrates the JSON configuration to achieve this result:

  • "action": "setValue",
  • "actionInput": {
  • "DateTripCompleted": "=getYear(@now)+'-'+padStart(toString(getMonth(@now)+1),2,'0')+'-'+padStart(toString(getDate(@now)),2,'0')"
  • }

Michel Mendes provides an example code where a field named 'Date Trip Completed' is formatted to reflect the current date upon clicking a button. If a date is already present, the formatted date is displayed and the 'Mark as complete' button is not shown.

The provided JSON example can be applied to implement the aforementioned feature by making sure to replace the field name as needed. Below is the JSON structure you can use:

With this SharePoint List Formatting template applied to a column, users can effectively set a date field to the current date with the click of a button. While the detailed JSON structure and elements are omitted for brevity, the concept showcases the power of customization in SharePoint Lists.

References are provided to the formatting syntax reference documentation on Microsoft Learn for readers who seek to dive deeper into List Formatting in SharePoint. However, external links and animated examples are excluded from this summary.

Read the full article Setting a SharePoint Date only field value to the current date using List Formatting JSON

 

SharePoint List Formatting - Auto-Set Current Date in SharePoint with JSON Formatting

 

People also ask

How do I format a date column in SharePoint JSON?

To format a date column in SharePoint JSON, you would use the "DateTime" mode in the "Column Formatting" section. By specifying the "format" element with an appropriate date string pattern, you can customize the display of the date in your SharePoint list or library. For example, use the format "toLocaleDateString()" to transform the date into a readable string based on the local date format.

How do I create a current date column in SharePoint?

To create a current date column in SharePoint, you will need to add a calculated column to your SharePoint list or library. In the formula section of the calculated column, you can use the [Today] shortcut to automatically use the current date. Please note that this calculated field will only be updated when an item is edited or added, not each day automatically.

How do I get only the date in a SharePoint list?

Getting only the date in a SharePoint list, without the time, requires adjusting the settings of your Date and Time column. Go to the list settings, click on the Date and Time column you wish to adjust, and set the "Date and Time Format" to "Date Only." This ensures that only the date part will be displayed and used throughout your list.

How do I format the date column in a SharePoint list?

To format the date column in a SharePoint list, access the list's column settings and modify the "Column Formatting" section by providing a custom JSON object. Here, you can utilize predefined tokens and patterns to control how your dates are displayed, such as "yyyy-MM-dd" for an international standard date format or "MM/dd/yyyy" for a more US-centric format.

 

Keywords

SharePoint List Formatting, SharePoint Date Field, Current Date JSON, SharePoint JSON Formatting, Set SharePoint Date, Current Date List Field, SharePoint Field Customization, SharePoint Date Automation, SharePoint JSON Date, SharePoint List Date Update