Get multiple nodes by URL
# help-with-umbraco
a
Does anyone have any suggestions on how I can retrieve one or more nodes by their url's, using JavaScript? Say I have an array of URLS, then I would want to retrieve all corresponding Umbraco nodes which correlate to these urls (using umbraco v12)
s
Sounds like a job for the Content Delivery API: https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api Once you've enabled it you can query by path:
/umbraco/delivery/api/v1/content/item/{path}
a
Thank you 🙂 Would you say it is better to loop through all url's and call its respective endpoint, if there are more than one, or is there some better way to just run one query and get all nodes back ( if possible ) ?
s
I am not sure what you're doing, but it seems to be the wrong way around? Your JS app "knows" all your URLs somehow? Why not get the content items and use the URLs from there.
a
@User We have integrated a map sollution from ArcGIS where some of the map features have some metadata containing URL's to be able to say which map item belongs to which umbraco item. So basically...whenever the map is being displayed, we check the layers being shown..and if any of these layers have umbraco url's in em..we need to get data from the node belonging to this url... probably poorly explained..but yeah..we only know the url string and nothing more..so we need to get the rest of the data from the api 🙂
s
Yikes.. well getting them 1 by 1 from the api is not a great idea, personally, I'd probably look into creating an APIController that does a more efficient lookup that will recieve the whole list of URLs (but I wouldn't know off the top of my head where to start). Is there no way to enrich the URL data (which clearly came from Umbraco at some point) with a list of content GUIDs maybe?
a
The main issue is that the urls contained within the map item is just a manual added string as the intial way of somehow connecting map items with cms content.😟 As of now, we request whatever items are currently being displayed in the map view ( updates on zoom / map navigation ).
s
I hope nobody renames any of those nodes and mess up the URLs then 😅
a
Yeah...that IS a major weakness in the setup 😛 If we had a node ID instead, would that open any other doors in regards of getting multiple content items in one go? 🤔
Perhaps using UmbracoHelper(?) : var contents = ids.Select(id => umbracoHelper.TypedContent(id)).ToList();
s
The Id won't change (unless deleted of course) so yes that should be pretty safe. And yes, you could pretty quickly get all content by a list of ids. I think you can even do
umbracoHelper.Content(new List<int> { 1, 2, 3, 4, 5, });
(
TypedContent
is just
Content
since v9).
a
Gonna give that a go first and see if this could help a bit on the performance. Thank you for your time and help, @Sebastiaan 🙂
s
No worries!
14 Views