Yluna
Yluna

Reputation: 103

SharePoint lookup field format link to list item its referencing

So I have two SharePoint lists: 1. Student: - Name - ... - ... 2. Company: - ... - Student (lookup field, Student list)

I wanted to format this 'Student name' field, changing text color and background color. This works, but now the field is no longer clickable. If I delete the custom formating, I can click that field value and it takes me to that student's details.

I tried to add the href attribute on the formating json but it doesn't work, I can't click it.

This is what I got:

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "@currentField.lookupValue",
"attributes": {
  "target": "_blank",
  "href": "='/sites/xxxx/Lists/Student/DispForm.aspx?Name=' + [$Student]"
},
"style": {
  "color": "#796BB1",
  "font-weight": "bold"
}

}

I also tried changing the link to 'www.google.com' in case the link to that item is wrong.

Upvotes: 0

Views: 2065

Answers (1)

Michael Han
Michael Han

Reputation: 3655

For hyperlinks, the elmType should be "a" instead of "div". You could try the below code:

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField.lookupValue",
 "attributes": {
      "href": {
         "operator": "+",
         "operands": [
            "/sites/michael/Lists/Student/DispForm.aspx?ID=",
            "@currentField.lookupId"
         ]
      },
      "target": "_blank"
   },
"style": {
  "color": "#796BB1",
  "font-weight": "bold"
}
}

Upvotes: 1

Related Questions