I have the following code given by Michael Han that is working just fine. It formats a number like '2030' to be '20:30'
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": {
"operator": "+",
"operands": [
"=substring(toString(#currentField),0,2)",
":",
"=substring(toString(#currentField),2,4)"
]
}
}
I need to use this column as a lookup column for another list, so I pasted this code and replaced #currentField with #currentField.LookupValue in the new lookup column as mentioned in https://github.com/SharePoint/sp-dev-docs/blob/master/docs/declarative-customization/column-formatting.md
The result is just showing ":". What do I have to do to make it work?
Regards,
Elio Fernandes
First, you need to use #currentField.lookupValue instead of #currentField.LookupValue, the first character of lookupValue should be lowercase.
And the code only works if the field in the parent list is Single line of text.
If the type of field in the parent list is Number, you need to change the code to this:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": {
"operator": "+",
"operands": [
"=substring(toString(#currentField.lookupValue),0,1)",
"=substring(toString(#currentField.lookupValue),2,3)",
":",
"=substring(toString(#currentField.lookupValue)3,5)"
]
}
}
Related
I'm trying to show multiple columns in one line of data in my form via a dropdown Menu.
Right now, it's just showing the Title and I can change it. I want to format it to :
ID + " - " + Title
I saw the Display Column Formatting in JSON at the EDIT Column in SPO but I don't know if thats what I'm looking for.
Thanks !
Try the below JSON code.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": {
"operator": "+",
"operands": [
"[$ID]",
"-",
"[$Title]"
]
}
}
I have the following in my column format, however the text does not turn red
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-
formatting.schema.json",
"elmType": "div",
"txtContent": "#currentField",
"style": {
"color": "=if([$Request_x0020_Date] >= #now + 432000000, '#ff0000', '')"
}
}
I have data in my list form 7/15, which is greater than 5 days and items from today. If I change the less than, greater than symbols I can get the text to turn red, but it turns it all red.
Please help!
As per my test, below JSON code works well on my end.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-
formatting.schema.json",
"elmType": "div",
"txtContent": "#currentField",
"style": {
"color": "=if([$RequestDate] >= #now + 432000000, '#ff0000', '')"
}
}
Pay attention to column variation $Request_x0020_Date in your code, you have to use the internal name of the column after "$". You can get the internal name of the column via List settings -> Columns section -> Click the column name to edit. You can find the internal name from the URL. For example, "RequestDate" after "Field=" is the internal name of my column "Request Date".
I have a lookup column in SharePoint 2019 that is looking back to a number field from a custom list.
I used the following JSON to format the lookup column to display the number without the hyperlink:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "#currentField.lookupValue"
}
I want to also remove the thousands commas to display the numbers from 1,234 to just 1234
Any suggestions on how this can be done simply? Seems odd there is no simple option to change the view format. (P.S. my JSON coding ability isn't great!)
There are a couple of ways to achieve this, some are a bit hack-ish but work:
if you are using Modern List Experience you can simply achieve this by applying following conditional formatting JSON to the number column in your lookup list:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"attributes": {
"class": "=if(#currentField > 0,'', '')"
},
"children": [
{
"elmType": "span",
"style": {
"display": "inline-block"
}
},
{
"elmType": "span",
"txtContent": "#currentField"
}
]
}
For Classic Experience a workaround could be to keep the data type of your number column as single line of text in your lookup list and apply the following formula in column validation section:
=ISNUMBER([text-column-name]+0)
hope this helps.
You could try the below json code:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": {
"operator": "+",
"operands": [
"=substring(#currentField.lookupValue,0,1)",
"=substring(#currentField.lookupValue,2,6)"
]
}
}
Change txtContent to =replace(#currentField.lookupValue, ',', ''):
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "=replace(#currentField.lookupValue, ',', '')"
}
I am very new to all this. I dont really know what most of these codes are, but with a little searching and trial an error I achieve my goals by changing bits and pieces. My current goal is to format a fields background color depending on the value of that field.
I found a very nice post on the subject. The problem is I need to combine a couple examples and I am having a problem. Here is the post.
https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting.
I have a date field called "Proposal Due Date". When the date is past, I want the BG one color. When the date is approaching (5 working days out) I want the BG another.
Below are the 2 examples I found and edited. I can only do one or the other. I need them both in the same format column box.
I can do the first part easy. It makes the BG a redish color if the date is past... Except it included todays date.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"debugMode": true,
"txtContent": "#currentField",
"style": {
"background-color": "=if([$Proposal_x0020_Due_x0020_Date] < #now, '#FAAAAA', '')"
}
}
The second part is a bit tricky for me. I dont know how to make this check 5 working days ahead instead of just 5 days. I also dont know how to prevent it from including days past.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"txtContent": "#currentField",
"style": {
"background-color": {
"operator": "?",
"operands": [
{
"operator": "<=",
"operands": [
"[$Proposal_x0020_Due_x0020_Date]",
{
"operator": "+",
"operands": [
"#now",
432000000
]
}
]
},
"#FCFC00",
""
]
}
}
}
I was looking for a way if JSON Script can look other column and format other column. base on the value of the other column.
Is there an alternate way to use "#currentfield"?
Thanks
Check the Apply formatting based on date ranges demo.
{
"elmType": "div",
"txtContent": "#currentField",
"style": {
"color": {
"operator": "?",
"operands": [
{
"operator": "<=",
"operands": [
"[$DueDate]",
"#now"
]
},
"#ff0000",
""
]
}
}
}
The MS Docs link #Lee_MSFT provided has some decent examples on formatting. In addition to the notes on internal/field names in the discussion on date ranges, the doc includes instructions for cross-referencing other columns (https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting#fieldname). Essentially, if you want to reference/use data from column A to format column B, you will need to use the "internal name" of column A in the JSON formatting on column B. A column name may be (and often is) different than the "display name" of the column. For example, we have a basic list for tracking some team members:
"Alias/Logon" (SLOT); internal name: Title
"First Name" (SLOT); internal name: FirstName
"Last Name" (SLOT); internal name: LastName
"Current Contracts" (SLOT); internal name: CurrentContracts
We want "Current Contracts" to be a URL link that uses the "Alias/Logon" value and we also want to display an icon. In fact, this column doesn't even require content as it will show an actual icon and the word "Contracts" for all users. Obviously, if you reference a column that is not mandatory, you should do some data validation to ensure you aren't creating bad URLs, etc.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"attributes": {
"target": "_blank",
"iconName": "BulletedList",
"class": "sp-field-quickActions",
"href": {
"operator": "+",
"operands": [
"https://mycompany.sharepoint.com/teams/coolpeople/contracts/",
"[$Title]"
]
}
},
"txtContent": "Contracts"
}
Note: just do a search for "SharePoint JSON column formatting" to find a plethora of examples, including the repo on GitHub (https://github.com/SharePoint/sp-dev-list-formatting).