Forum Discussion

MonkeyManvin's avatar
MonkeyManvin
Copper Contributor
Feb 04, 2021
Solved

Customise library column to be link

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&FilterValue1=' + @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

  • ganeshsanap's avatar
    ganeshsanap
    Feb 04, 2021

    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.

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.

    • MonkeyManvin's avatar
      MonkeyManvin
      Copper Contributor

      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

      • ganeshsanap's avatar
        ganeshsanap
        MVP

        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.

Resources