Forum Discussion

EmberK's avatar
EmberK
Copper Contributor
Jan 03, 2024
Solved

SP field to allow for entry of year only but set default value to year of item created

Greetings - I am trying to see if it is possible to create a SP library column that would allow for the entry of a 4 digits/characters (to represent Year) but have the default value equal to the year from the items created date.

 

Use case...document library for reports with the request to creating grouping by Year.  Generally the Year will equal the Year from the item's created date (when the item was loaded), but there will be instances when previous years reports or end of year reports loaded in the following year (i.e. December 2023 loaded in January 2024) will be added and we will need the ability to override the default value.  Am trying to assist with multiple document loading by setting the default value, which will account for the majority of the loads, but need the override option.

 

Looking for advice on:

1. is this even possible?

2. if it is possible, what should the field type be?

 

I have tried starting with text, number and date fields, and utilizing the default value option to be various formula iterations to extract the YEAR from the [Created] date field.  In all cases I get either the error that the Default Value is not a number/text or just a general syntax error as it doesn't even like the formula.  To overcome the not a number/text value error, I have also tried iterations of converting said result to number/text.  

 

Any assistance or further research direction greatly appreciated.

  • So for those who may have a similar issue, here is how I resolved this question (not sure if it is the best/only way, but this is where I landed).

     

    I think I discovered that while you can use Created Date as an operator in a calculated field, you cannot use it when setting an items default value.  Due to that, here was my solution...

     

    Created the new Date and Time column and set its default value to =TODAY().  Then used JSON column formatting (shown below) to display the field with the Year only.  This solution allows a user to accept the default value to be equal to the year in which they loaded the item, or edit the field and enter a different date.  This solution does require the user to enter a full date (m/d/yy) versus my idea to just collect the year, but the final result displays as I had envisioned.

     

    JSON script:

    {
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "span",
    "txtContent": "=getYear(@currentField)"
    }

1 Reply

  • EmberK's avatar
    EmberK
    Copper Contributor

    So for those who may have a similar issue, here is how I resolved this question (not sure if it is the best/only way, but this is where I landed).

     

    I think I discovered that while you can use Created Date as an operator in a calculated field, you cannot use it when setting an items default value.  Due to that, here was my solution...

     

    Created the new Date and Time column and set its default value to =TODAY().  Then used JSON column formatting (shown below) to display the field with the Year only.  This solution allows a user to accept the default value to be equal to the year in which they loaded the item, or edit the field and enter a different date.  This solution does require the user to enter a full date (m/d/yy) versus my idea to just collect the year, but the final result displays as I had envisioned.

     

    JSON script:

    {
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "span",
    "txtContent": "=getYear(@currentField)"
    }

Resources