Reputation:
I was a beginner at Umbraco CMS, and I noticed that every time I need to query for node, I am relying on constant id's.
For example:
public static class Constants
{
public static class Pages
{
public static class System
{
public const int Root = 1059;
public const int Home = 1092;
public const int Pressemeddelelse = 1143;
}
public const int GratisKontoComplete = 1181;
public const int TilmeldNyhedsbrevComplete = 1182;
}
}
The above code helps me when I need to get the node, like @Umbraco.NiceUrl(Constants.Pages.System.Root);
Now my question is:
Is there any other way how to achieve this? What if the content editor deleted the node and created? The node if will changed for sure right. Do you want me to query it via NodeAlias? but I dont know how, I dont want to use uQuery because I am using the latest Umbraco Version 7.4.3 using MVC.
Upvotes: 1
Views: 1640
Reputation: 374
First of all you should not to use the ids for retrieving nodes. there are many ways but for me the most easy and reliable way is to use linq query. For example there are three nodes HOME, BLOG and COMMENT .Now home is parent node and BLOG and Comment are child nodes of HOME node. Now to retrieve the parent node use the following node.
Umbraco.TypedContentAtRoot().First();
or
Model.Content.AncestorOrSelf(1).First()
where model is the current node . You can also use this code too
var rootNode = new Node(-1);
All the aboce codes will help you to get the root node i.e HOME node , now after getting the root node you can now use SQL queries to get all the children nodes of HOME node and so on . FOr example if I want to retrieve BLOG node then my code will be
'var Blog= rootNode.Children.First(x => x.DocumentTypeAlias == "Blog");'
where "DocumentTypeAlias" is the alias name of BLOG node. Hopefully this will help you
Cheers
Upvotes: 5