Forum Discussion
Custom column formatting: get current folder (for hyperlink with relative path)
I am using this article as the basis for applying custom formatting to create a hyperlink from the field value.
So the JSON for formatting is something like:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "a",
"txtContent": "Project Documents",
"attributes": {
"target": "_blank",
"href": "='https://crescent.sharepoint.com/sites/projects/' + @currentField"
}
}
In the above, @currentField is used to extract the value of the current field/column.
And I see from here that it's also possible to use @currentWeb get the main site dynamically rather than hardcode it.
What I really need is something similar to get the folder for the current item, so that I can for example then refer to a sub-folder relative to the current folder, rather than relative to the top level, i.e. something like @currentFolder in the below:
"href": "=@currentWeb + '/' + @currentFolder + '/my-sub-folder/' + @currentField"
Is this possible? Or is there a better way to construct hyperlinks like this that are relative to the current item?
The problem with full hard-coded paths (like the following) is that this formatting trick won't work for any folder, only for a specific folder...
"href": "=@currentWeb + '/Documents/North Dept/Team A/2023/Reports/my-sub-folder/' + @currentField"
And I can't see that using an actual Hyperlink type of column would work for this, because the full path name would need to be used... because it's just a string that is interpreted as a URL? I'd really like the ability to create hyperlinks that are relative to the current folder.