Search code examples
jsonsharepointsharepoint-onlinesharepoint-list

SharePoint List: How to use indexOf(@currentField, 'target') when field type is 'Hyperlink or Picture'


In a SharePoint List, I have a field of type 'Hyperlink or Picture' that has a URL like 'https://www.msn.com'.

If the field is type text, I can do things like:

indexOf(@currentField, 'msn.com') > 0

If the field is type 'Hyperlink or Picture', this does not work.

What am I missing?

Update: In the list view, it looks correct (note the correct icons): enter image description here

When I add it to a page, the icons are not correct BUT the text is: enter image description here


Solution

  • I just tested this on our SharePoint online site with hyperlink column in list. Here are the results:

    JSON used:

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=if(indexOf([$Hyperlink], 'msn.com') > -1, 'Found MSN', 'Not found')"
    }
    

    Where Hyperlink is the internal name of your SharePoint list column. You can get the internal name of your SharePoint list columns by following this article: How to find the Internal name of columns in SharePoint Online?

    Output:

    enter image description here

    So, you should be able to check for any substring in "link" in hyperlink column using expression like:

    indexOf([$Hyperlink], 'msn.com') > -1
    

    If you want to check for any substring in "description/display" text of hyperlink column, use JSON like:

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=if(indexOf([$Hyperlink.desc], 'MSN') > -1, 'Found MSN', 'Not found')"
    }
    

    Output:

    enter image description here

    Note: indexOf operator is case-sensitive