Reputation: 809
My Content structure is:
-Home (the site root node) -About Us -Our Sevice1 -Our Sevice2 -Our Sevice3
I created a macro for Our Services. In macro, I want Our Sevice1, Our Sevice2, Our Sevice3... But in the list variable About Us also come but I don't want it I want only our service name of the child node
var list= CurrentPage.Children();
About Us also come on the list but I don't want it.
Upvotes: 1
Views: 1709
Reputation: 2915
Like @Mivaweb mentioned, it's better to not use dynamics (I think for performance in addition to being removed in the future).
However, I don't think you have to create a separate doc type, although that will work too. The predicate for the Where method should handle other expressions such as:
var servicePages = Model.Content.Children.Where(x => x.Name.StartsWith("Our Sevice"));
Upvotes: 0
Reputation: 5722
The reason that you see the About Us
page in the collection is because you use the Children
method.
With the Children
method you ask for the direct child nodes of a parent node traversing one level down. So in this case you ask for all direct children of the home page so this works like expected.
What you are trying to achieve is a collection of of all Service
nodes. To accomplish this you could do something like this.
Make sure that you have a seperated Document Type for your service nodes ( like for example doc type Service Page
).
Then you can do the following:
var servicePages = CurrentPage.ServicePages;
You can view the docs about it here:
https://our.umbraco.org/documentation/reference/querying/dynamicpublishedcontent/collections
But all of this is using dynamic syntax, this will be removed in future versions of Umbraco. So I suggest you go and use the strongly type syntax.
Then this can be changed by:
var servicePages = Model.Content.Children.Where(x => x.DocmentTypeAlias == "servicePage");
What this does is take the IPublishedContent
object of the current page you are on, which is the Home Page then you take all children which has a document type alias of type servicePage
.
Upvotes: 1