Creating a custom column format in List

Gannon, James DDS St. Paul 0 Reputation points
2023-08-24T15:05:32.08+00:00

I am trying to create a custom format for a SharePoint list column that includes leading zeros in a number format. For example, I have employee numbers that are formatted as 0012, 0123, 0235, etc. and they are only ever 4 digits.

As seen here, the first few should display: 0146, 0001, 0002, (blank), 0006, and so on.

User's image

When the numbers are entered with the zeros, they are automatically removed. I want the leading 0's to stay. Any help appreciated! Just got started being our SharePoint Admin from scratch. I've used the custom column formatting before, so I know where to add that but the code is not working as I want it to.

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 51,941 Reputation points Microsoft External Staff
    2023-08-25T02:16:34.2666667+00:00

    Click the number column -> Column settings -> Format this column -> Advanced mode -> Use following codes.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=if(@currentField == '', '', padStart(toString(@currentField),4,'0'))"
    }
    

    Result:

    enter image description here


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.