SOLVED

Displaying DocLib File URL Path in Column

Copper Contributor

Good day,

Using SharePoint Online (modern) and set up a new document library called DOCLIB1.  There are two folders in the library called FOLDER1 and FOLDER2.  FILE1.TXT and FILE2.TXT are located within FOLDER1 (and are displayed by those names in the SharePoint default "NAME" column).  There is also a FILE2.TXT (same name but different content) in FOLDER2.

 

Searching the DocLib for anything called "FILE2", I see two results, but cannot tell where the files are located (cannot tell which is which).  

 

I created a new column called PATH, with a Type of "Single line of text". Can I display a file path in this new column to see something like "FOLDER1/FILE2.TXT" for the first search result returned, and "FOLDER2/FILE2.TXT" for the next?  

 

Thanks!

5 Replies

@KevBel You can run a Power automate flow on file creation / update (as per your requirements) and then update the new PATH column from your flow OR use JSON formatting for showing the file path. However, I am not sure if JSON formatting will work in search results page or not (I have to test it from my end).


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

best response confirmed by KevBel (Copper Contributor)
Solution

Hi @KevBel,

you could apply the following json formatting to your "Path" column

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=[$FileRef]"
}


You will get a view like this

SvenSieverding_0-1689679569656.png

If you don't want the path to your document library to be displayed ("/sites/MyAppPage/CopyLib/" in my example), then use this json

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=replace([$FileRef],'/sites/MyAppPage/CopyLib/','')"
}

 This will look like this

SvenSieverding_1-1689679805016.png

 

Best Regards,
Sven

That second example is perfect for my needs - thank you!

Would there be a way to remove the file name from that path view as well? Seeing the output, it looks a bit redundant to show the file name again since it already exists in the NAME column.

Hi @KevBel,

in the SharePoint data model, the NAME column is called FileLeafRef. We can replace that value with an empty string like this

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=replace(replace([$FileRef],'/sites/MyAppPage/CopyLib/',''),[$FileLeafRef],'')"
}

 
Best Regards,
Sven

There is a built-in column called Path that shows the Path nicely formatted and every folder and subfolder is clickable. The Path is relative to the sharepoint site (web). All you need is to add this "Path" column to a view programmatically (PnP, Rest API), I have an example on m blog post: https://chuvash.eu/2023/show-path-column/
1 best response

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

Hi @KevBel,

you could apply the following json formatting to your "Path" column

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=[$FileRef]"
}


You will get a view like this

SvenSieverding_0-1689679569656.png

If you don't want the path to your document library to be displayed ("/sites/MyAppPage/CopyLib/" in my example), then use this json

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=replace([$FileRef],'/sites/MyAppPage/CopyLib/','')"
}

 This will look like this

SvenSieverding_1-1689679805016.png

 

Best Regards,
Sven

View solution in original post