Umbraco - Select node by URL
Asked Answered
V

1

5

I'm having issues with Umbraco when trying to get access to a node by it's url.

I've been trying to form an xpath query to select on the url, but I've no idea how to do that, and everything in their API is a 'dynamic' so I'm finding it impossible to dig much deeper for other methods.

The "Link to document" property in the Umbraco editor shows the path to be "/links/link-regions/link-region-1/". Its location in the editor is "/Data/Links/Link Regions/Link Region 1".

I suspect it'll look like this, but I cna't make it work:

//*[@url='/links/link-regions/link-region-1/']

I basically want to know what I have to do to get the node by providing either of these values and no others. I've been trying to do this for days now to no avail, please help!

Vendue answered 27/6, 2014 at 11:30 Comment(1)
The cheatsheets found here may be useful when dealing with dynamic types - linkEngineer
E
16

If you're using Umbraco 4.11 or later, there are a new set of APIs that allow you to retrieve strongly-typed content (as opposed to dynamic - which can be frustrating when trying to evaluate types when debugging).

UmbracoContext.Current.ContentCache.GetByRoute(string url) should do what you want.

Epi answered 28/6, 2014 at 8:43 Comment(2)
Awesome, thanks! I'll have a go on Monday when I'm back in work. I'll let you know how I get on.Vendue
That works perfectly. Thanks so much for that, wasted so many hours looking for it last week. I really wish stuff like that was documented somewhere other than with people asking questions on here.Vendue

© 2022 - 2024 — McMap. All rights reserved.