SOLVED

Customise library column to be link

Copper Contributor

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

6 Replies

@MonkeyManvin 

 

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.

@ganeshsanap 

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

best response confirmed by MonkeyManvin (Copper Contributor)
Solution

@MonkeyManvin 

 

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.

@ganeshsanap Thanks again - that's perfect

 

Does this mean you cant reference other column when configuring a column?

Just tested and i can just ref by $fieldname! :) But Name doesnt work - i presume it has a different name under the hood?

@MonkeyManvin 

 

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

1 best response

Accepted Solutions
best response confirmed by MonkeyManvin (Copper Contributor)
Solution

@MonkeyManvin 

 

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.

View solution in original post