Umbraco 7 + Razor: How to get a document / Node by ID?

In the sample code, you will get the home page from the .cshtml file

var homePage = CurrentPage.AncestorsOrSelf(1).First(); 

Now, how do I get a specific page / node (not related to the current page, such as a news page) by its identifier?

+6
source share
2 answers

You can use exactly the same approach, but use .Where(x => x.Id == newsPageId) .

Alternatively, you can use @Umbraco.TypedContent(newsPageId) or @Umbraco.Content(newsPageId) .

+16
source

The direct answer is Umbraco.TypedContent (id). But! If you want to get the β€œNews” node from anywhere, I recommend the following. Using ids is problematic for some multilingual settings, and if the identifier changes, it will stop working.

 // 1- Get root node var site = Model.Content.AncestorOrSelf("Site"); // 2- Get news node var news = site.Descendant("News"); 

This approach is more dynamic, and now you can use your node news to loop it over children or whatever you need. For this you need special types of documents for site types and news.

Hope this helps!

+7
source

Source: https://habr.com/ru/post/974874/


All Articles