1

I am trying to create a document library with multiple values in a lookup field function as hyperlinks to the respective documents. By default the lookup column navigates to a details page, this is not what I wanted. I tried the below code to represent the docs as links (but this only works if there is only 1 document).

When multiple documents are selected in the lookup column, the hyperlink is still only one link, which fails.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField.lookupValue",
  "attributes": {
    "target": "_blank",
    "href": "='https://.sharepoint.com/sites/sitename/libraryname/' + @currentField.lookupValue"
  }
}

My second attempt was the example below. This runs the foreach if there are multiple lookup selections, but my result is creating a result of [object Object] for the title and URL.

I am assuming I need a different operator for a lookup column.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "forEach": "choiceIterator in @currentField",
      "elmType": "a",
      "txtContent": "[$choiceIterator]",
      "attributes": {
        "target": "_blank",
        "href": "='https://.sharepoint.com/sites/sitename/libraryname/' + [$choiceIterator]"
      },
      "style": {
        "width": "80px",
        "height": "15px",
        "text-align": "center",
        "padding": "0px",
        "margin": "1px"
      }
    }
  ]
}

enter image description here

enter image description here

I would like to know how to allow a multi value lookup column to function as a hyperlink to each of the documents selected using their title as the display title (individually).

Additionally, I would like to allow for a dynamic URL which will adapt to which folder the documents are currently in (my current examples are assuming a static non-folder structure.

3
  • Are you using lookup column from library to list? Are you using "Title" from library as a main column in lookup settings? Commented Sep 2, 2022 at 12:55
  • Can you add screenshot of lookup column settings to your question? Commented Sep 2, 2022 at 13:01
  • I am using the lookup column from library to same library (goal is the documents are all stored in a library and are related to each other, one as the primary, and others as secondary). I have added a picture to the initial post
    – nripp
    Commented Sep 2, 2022 at 14:10

1 Answer 1

0

I don't think it is possible to achieve your requirements using JSON formatting as you cannot retrieve URL of document using JSON formatting.

As Name or File Path properties are not supported in lookup settings and also in JSON formatting, you cannot get the dynamic URL of documents using JSON formatting.


You can create multiple links by modifying your JSON like this:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "forEach": "choiceIterator in @currentField",
            "elmType": "a",
            "txtContent": "[$choiceIterator.lookupValue]",
            "attributes": {
                "target": "_blank",
                "href": "='https://.sharepoint.com/sites/sitename/libraryname/' + [$choiceIterator.lookupValue]"
            },
            "style": {
                "width": "100%"
            }
        }
    ]
}

Output:

enter image description here

But, it will not open the document as you are using "Title" column as a main column in lookup settings. Title column is not always same as file name (Name column), so it will not return the correct URL of document in JSON.

5
  • Thank you for the adjustment suggestion. I have implemented it with success for the first goal. I am using a Power Automate flow to collect emails and their attachments, in this process I am making sure th title is updated with the name, so the static hyperlink works. Is there any way to dynamically grab the folder name to add to the href link construction? or have any any additional thoughts on how to achieve this?
    – nripp
    Commented Sep 2, 2022 at 14:39
  • If I were to add a column which would be populated with my Power Automate flow upon document creation that matches my folder name, could I pull another columns text into this JSON formatting for the href construction?
    – nripp
    Commented Sep 2, 2022 at 14:59
  • Yes, if you create another column which will hold the folder URL path, you can use it in href in JSON to construct the complete URL. But what if multiple child links are selected from multiple folders? Commented Sep 2, 2022 at 15:17
  • Since this is an email + attachments grab, the email and associate attachment docs will all be placed in their email domain folder and should have the same url/folder name. I will test and verify the requirements. I think I can use a text or metadata column to add value for refinement/search as well as help to build the href.
    – nripp
    Commented Sep 2, 2022 at 15:23
  • Then you can create a text column, update folder URL path in text field using power Automate. Then you can use the text field to build href URL in JSON. Commented Sep 2, 2022 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.