Creating links in properties using dataviewjs

Hello Obsidian community,

in my file properties, I have a list that shows links. Normally I fill them in by hand, but I thought it must be possible to let them fill in automatically with the help of dataviewjs.
When I try $=dv.span(dv.pages('"Folder/Subfolder"').file.link) it works fine. When I try the same inside the properties, it does not work; code below.
I switched to code mode, edited the properties section and switched back to reading mode. In the reading mode, the whole property section text is red and does not work any more.

Is there any chance to automate this?

Thank you in advance
Ferry

%—
%tags:
% - AAA
% - BBB
% Links: $=dv.span(dv.pages('"Folder/Subfolder"').file.link)
%—

You are allowed to store the query within the properties, if you do something like: Links: '`$= dv.span(“testing”)`' . But this will store the query, not the result of the query.

To get a dataviewjs result into the properties, I’ve come across mostly two approaches. 1) Use a template to execute the query, and insert the result, 2) Use a query which uses app.fileManager.processFrontMatter() to insert stuff into the properties.

The first approach has the downside that it’s a one time happening when you insert the template. To get any updates related to your query, you need to remove the old stuff, and reinsert/execute the template again.

The second approach has the downside that it’ll update the properties whenever the query is re-run (like when opening the page or rebuilding the view or changing the query), and you need to use asynchronous functions to achieve the update which in some cases might cause some issues with race conditions when updating the file in question.

Both approaches do(/can) properly update the properties with proper links, which Obsidian will insert into the backlink cache, and allow to be renamed, and so on.

Then again, you might not want it to appear in the link caches, and then it would be better to have the list outside of the properties all together in an ordinary query.

2 Likes

First of all, thank you for your advice!

I’m pretty new to Obsidian and Javaskript and I don’t really understand why this is not easy doable, because I think I’m probably not the only one with such types of problem?
Is there a specific strategic or technical reason why this does not work as easy as I thought it would be?

Thank you in advance
Ferry

It’s just not the way dataview is meant to be used. It’s not defined in it to do stuff like that.

However, you might find that the Metadata menu plugin can do stuff related to you requirements. In that plugin you can define a property to be a field of type file (or multi file), and supply that field definition with a query (which happens to be a dataview query) for which files to select the value from.

I’ve not used that plugin a lot, so I might be a little unprecise in that previous paragraph, but I do know it’s trying to do stuff like you want one way or another. In any case that plugin might be something you’d like to explore.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.