SOLVED

Conditional column formatting to create hyperlink to different sites based on item value

Copper Contributor

Hello, 

As in the subject- I would like to create hyperlinks to different sites under one column depending on the text value for the item on the list/library under that column. So for example item 1 is having value "X" under column "Y" and I want "X" to be hyperlinked to site "A", if the value of the item is "Z" I want hyperlink to site "Z" etc. Would someone be so nice and give an example?

2 Replies
best response confirmed by Veingro (Copper Contributor)
Solution

@Veingro 

Based on what I understood from your question, you need to use JSON in below format:

 

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "a",
    "txtContent": "@currentField",
    "attributes": {
        "href": "=if(@currentField == 'X', 'https://tenant.sharepoint.com/sites/A', if(@currentField == 'Z', 'https://tenant.sharepoint.com/sites/Z', 'https://tenant.sharepoint.com/sites/default'))",
        "target": "_blank"
    }
}

 

Add this code to column "Y" by selecting "Format this column" settings.

DocumentationUse column formatting to customize SharePoint 


Please click Mark as Best Response 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.

 

I am trying to do something similar, but with one additional twist:

I have two Sharepoint lists: "Projects" and "Items". Each project will have multiple items associated to it. So, imagine project "Ice Cream" has items ["Buy Ice Cream", "Eat Ice Cream", "Take Nap"]. All three of the items have a column called Master_ID which is populated with the Sharepoint ID of the parent project.

I would like to be able to click on the Project ID# and have it link to the Item list, but with the view filtered to show only the corresponding Items (Filter Master_ID).

I'm not a programmer, so I'm hoping someone can help me get my brain around it.

Thanks in advance!
1 best response

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

@Veingro 

Based on what I understood from your question, you need to use JSON in below format:

 

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "a",
    "txtContent": "@currentField",
    "attributes": {
        "href": "=if(@currentField == 'X', 'https://tenant.sharepoint.com/sites/A', if(@currentField == 'Z', 'https://tenant.sharepoint.com/sites/Z', 'https://tenant.sharepoint.com/sites/default'))",
        "target": "_blank"
    }
}

 

Add this code to column "Y" by selecting "Format this column" settings.

DocumentationUse column formatting to customize SharePoint 


Please click Mark as Best Response 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