Display number of Children for a specific name Node
Hi
Im using this code to display the number of children underneath a specific node. At the moment it is doing so via the node's ID. Is there a way to set it so it goes off the Node's Name instead?
code:
var MYnode = Umbraco.TypedContent(1562);
var MYchildAmount = MYnode.Children.Count();
Something like should work - you get the website root node and then work down from there:
var siteNode = Umbraco.TypedContentAtRoot().First();
var locationsNode = siteNode.Children.Where(x => x.Name == "Locations").FirstOrDefault();
var jobs = locationsNode.Children.Where(x => x.DocumentTypeAlias == "job").Count();
I'm guessing jobs are in a location (this might be department or whatever so change this to suit).
The last like counts the jobs - if you only have jobs under these containers you could remove the where check on the docType.
This is a simple example that does no null checking. You probably should check if the locations node exists on the count.
var jobs = locationsNode != null ? locationsNode.Children.Where(x => x.DocumentTypeAlias == "propertyArea").Count() : 0;
Display number of Children for a specific name Node
Hi
Im using this code to display the number of children underneath a specific node. At the moment it is doing so via the node's ID. Is there a way to set it so it goes off the Node's Name instead?
code:
var MYnode = Umbraco.TypedContent(1562); var MYchildAmount = MYnode.Children.Count();
Thanks
Hi Jools,
Something like should work - you get the website root node and then work down from there:
I'm guessing jobs are in a location (this might be department or whatever so change this to suit). The last like counts the jobs - if you only have jobs under these containers you could remove the where check on the docType.
This is a simple example that does no null checking. You probably should check if the locations node exists on the count.
Thanks Steve - that works great.
Im just trying to develop it further using a loop, but being a designer my razor is very limited.
So all i want to do, is if the @ITchildAmount = 0 then hide the 'job-info' div and show the 'no-jobs' div instead.
Trying to get a for loop going but doing something wrong.
Might you be able to point me in the right direction?
Thanks again
Actually scratch that I’ve cracked it and got it working :)
Good job - glad you sorted it!
is working on a reply...