Feb 04 2021 05:38 AM
Hi
I would like to customise a column in my SharePoint library to be a link but only if the value is 1. I also want the link to reference a different column for the document (Name).
So far i have this:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"attributes": {
"target": "_blank",
"href": "='https://our.sharepoint.com/sites/test/Lists/Log/AllItems.aspx?FilterField1=LinkTitleNoMenu&FilterVal...' + @nameField
},
"style": {
"border": "none",
"background-color": "transparent",
"cursor": "pointer"
},
"children": [
{
"elmType": "span",
"attributes": {
"iconName": "View",
"class": "ms-font-l"
}
}
]
}
Obviously i need the right code for the @namefield bit and the code to only be a link if the value of the field is 1.
Thanks in advance
Feb 04 2021 06:11 AM
You can get the Name column (document name) in JSON using [$FileLeafRef]. Also, you can show/hide the element created in JSON using "display" property in "style" attribute like:
"style": {
"display": "=if(@currentField == 1 , 'none' , 'block')"
}
I don't know all your requirements. But, as per my understanding by reading your question I have created below JSON code which might work for your requirements or you can modify accordingly:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"children": [
{
"elmType": "span",
"txtContent": "@currentField",
"style": {
"display": "=if(@currentField == 1 , 'none' , 'block')"
}
},
{
"elmType": "a",
"txtContent": "@currentField",
"attributes": {
"target": "_blank",
"href": "='https://our.sharepoint.com/sites/test/Lists/Log/AllItems.aspx?FilterField1=LinkTitleNoMenu&FilterVal...' + [$FileLeafRef]"
},
"style": {
"border": "none",
"background-color": "transparent",
"cursor": "pointer",
"display": "=if(@currentField == 1 , 'block' , 'none')"
},
"children": [
{
"elmType": "span",
"attributes": {
"iconName": "View",
"class": "ms-font-l"
}
}
]
}
]
}
This code checks the value of current field where you will add the JSON. If value is 1 then it will show as a link else simple text.
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
Feb 04 2021 09:40 AM
Of course! Didnt think of it in that way!
Your code is almost perfect - the only issue is that I need the Name of the file from SP not the file name with extention - ie I need FileName not FileName.docx
Feb 04 2021 10:11 AM
Solution
To get the File name without extension you can use:
substring([$FileLeafRef], 0, lastIndexOf('[$FileLeafRef]', '.'))
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
Feb 05 2021 03:44 AM
@ganeshsanap Thanks again - that's perfect
Does this mean you cant reference other column when configuring a column?
Feb 05 2021 03:49 AM
Feb 05 2021 03:56 AM - edited Apr 17 2023 06:15 AM
In JSON formatting, you have to use the internal name of columns. Display name which you see in list/list views will not work in JSON.
For more information about supported column types you can refer: JSON Formatting - Supported column types
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
For SharePoint/Power Platform blogs, visit: Ganesh Sanap Blogs
Feb 04 2021 10:11 AM
Solution
To get the File name without extension you can use:
substring([$FileLeafRef], 0, lastIndexOf('[$FileLeafRef]', '.'))
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.