Listing all child nodes of a certain document type
Hi all,
Realy hoping someone can help me with this one i have been struggling for a few days now.
I have the following tree structure in my umbraco v6.1.2 site
and i would like to display all the 3rd level node names in a dropdown list which is part of my c# user control. I am able to target a certain node and display its children in a dropdown using the following code (which i used for another dropdown to bind the driver names)
//If you need a specific node based on ID use this method (where 123 = the desired node id)
Node driverParentNode = new Node(1083);
//To get the children of the driverParentNode as a List collection and sort the nodes alpha
var driverChildNodes = driverParentNode.ChildrenAsList.OfType<Node>().OrderBy(n => n.Name);
//Create a new list to store the child nodes
List<string> finalDriverList = new List<string>();
//Iterating over nodes driver child nodes collection and add them to the final driver list
foreach (var node in driverChildNodes)
{
finalDriverList.Add(node.Name);
}
//display the final driver list in the dropdown control
ddlPreferedDriver.DataSource = finalDriverList;
ddlPreferedDriver.DataBind();
ddlPreferedDriver.Items.Insert(0, new ListItem("Select Driver...", "0"));
Using the same code i can get all the level two items into a dropdown list (mercedes,ford etc.) into the vehicle ddl but how can i get the children of these into the ddl?
Now i thought i could achieve this by targeting the VehicleManufacturer doc type(id=1184) which is what all second level nodes are created from. Unfortunately my efforts so far have failed and im unsure if this can even be achieved.
List<Node> foundNodes = new List<Node>();
DocumentType nodeId = new DocumentType(1184);
foreach (Node childNode in nodeId.Children)
{
var child = childNode;
if (child.NodeTypeAlias == "VehicleProfile")
{
foundNodes.Add(child);
}
}
//display the final driver list in the dropdown control
ddlPreferedVehicle.DataSource = foundNodes;
ddlPreferedVehicle.DataBind();
ddlPreferedVehicle.Items.Insert(0, new ListItem("Select Vehicle...", "0"));
If someone out there has any info which may help me achieve this then i would be really gratefull
First, ensure that your user control is inherited from UmbracoUserControl. This will allow easy access to the new APIs.
To get the Level 3 items, you would do something like this.
//Get the parent document
var driverParentNode = Umbraco.TypedContent(1234);
//All children at level 3
var childrenLevel3 = driverParentNode.Descendants().Where(x => x.Level == 3);
Hopefully this should get you pointed in the right direction. Please let me know if you have any problems :)
Thanks very much for getting back on this one at least it gives me something to work at :).
When you say that my user control needs to inherit from the umbraco UmbracoUserControl could you please give an example of this? What namespaces do I require for the new api v6 stuff is it
Using umbraco.Core.models ?
I have come across something similar to the code you supplied but the .descendants was not being recognised which may of been because I didn't have a reference to the namespace?
Sorry if I'm asking silly questions it's all stuff I haven't done much with.
No problems mate I am stopping for today and will check this out with a fresh head tomorrow 😄. I did stumble across this though
So to get started, you’ll need to make a reference to “umbraco.dll” in your Visual Studio project. When you use the ContentService through a User Control, it’s also a good idea to make a reference to “Umbraco.Core.dll”. This allows you to make your User Control inherit from “Umbraco.Web.UmbracoUserControl” instead of the normal “System.Web.UI.UserControl” and gives you super easy access to local Umbraco data as well as the ContentService.
Been looking into this on the wya into work this morning and i am having a problem with references. I cant seem to compile my code because there is a problem with the follwoing line
var driverParentNode1 = Umbraco.TypedContent(1234);
I have tried referencing the following namespaces but not had any luck
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.UI;
using Umbraco.Web.WebApi;
using Umbraco.Web.UI.Controls.UmbracoUserControl;
it doesnt seem to like the bottom one it is giving the following error
Error 1 A using namespace directive can only be applied to namespaces; 'Umbraco.Web.UI.Controls.UmbracoUserControl' is a type not a namespace
Here is an example usercontrol (only the cs file, the ascx file doesn't need changing, if I remeber correctly):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Extra namespaces
using Umbraco.Web.UI.Controls;
using Umbraco.Web;
//Using the full namespace for UmbracoUserControl is important, otherwise you will get an ambiguous reference
public partial class UserControls_TestUserControl : Umbraco.Web.UI.Controls.UmbracoUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the parent document
var driverParentNode = Umbraco.TypedContent(1234);
//All children at level 3
var childrenLevel3 = driverParentNode.Descendants().Where(x => x.Level == 3);
}
}
You'll need references to umbraco.dll, Umbraco.Core.dll and Umbraco.Web.UI.dll.
Just wanted to thank you for your help on this one i have managed to populate my ddl with the values of my vehicles :) using the code and info that you have supplied.
I think the issue that i was having was some missing references and some issues in my web config but a bit of juggling around and using a backed up version of my web config i was able to solve it.
For others who may be interested in this here is the code i used, i decided to do it by doc type instead of level. (hopefully its coded ok)
//create a list to store the final vehicle profile list in
List<string> finalVehicleList = new List<string>();
//Get the parent node (vehicle Area)
var driverParentNode1 = Umbraco.TypedContent(1187);
//Get all vehicle profiles by doc type and order them alphabetically
var vehicleProfile = driverParentNode1.Descendants().Where(x => x.DocumentTypeAlias == "VehicleProfile").OrderBy(n => n.Name);
//loop through the vehicle profile list and and them to the final vehicle list
foreach (var item in vehicleProfile)
{
finalVehicleList.Add(item.Name);
}
//display the vehicle list in the prefered vehicle dropdown control
ddlPreferedVehicle.DataSource = finalVehicleList;
ddlPreferedVehicle.DataBind();
ddlPreferedVehicle.Items.Insert(0, new ListItem("Select Vehicle...", "0"));
}
Couldnt of done this without your help mate so thank you very much for that
Listing all child nodes of a certain document type
Hi all,
Realy hoping someone can help me with this one i have been struggling for a few days now.
I have the following tree structure in my umbraco v6.1.2 site
and i would like to display all the 3rd level node names in a dropdown list which is part of my c# user control. I am able to target a certain node and display its children in a dropdown using the following code (which i used for another dropdown to bind the driver names)
Using the same code i can get all the level two items into a dropdown list (mercedes,ford etc.) into the vehicle ddl but how can i get the children of these into the ddl?
Now i thought i could achieve this by targeting the VehicleManufacturer doc type(id=1184) which is what all second level nodes are created from. Unfortunately my efforts so far have failed and im unsure if this can even be achieved.
If someone out there has any info which may help me achieve this then i would be really gratefull
cheers all
Paul
First off, you are using the old, deprecated v4 API. You should really be using the new v6 API instead, which is what I'll be using in this post. See https://our.umbraco.org/documentation/Reference/Templating/Mvc/querying for some docs.
First, ensure that your user control is inherited from
UmbracoUserControl
. This will allow easy access to the new APIs.To get the Level 3 items, you would do something like this.
Hopefully this should get you pointed in the right direction. Please let me know if you have any problems :)
Asbjørn
Hi Asbjørn,
Thanks very much for getting back on this one at least it gives me something to work at :).
When you say that my user control needs to inherit from the umbraco UmbracoUserControl could you please give an example of this? What namespaces do I require for the new api v6 stuff is it
Using umbraco.Core.models ?
I have come across something similar to the code you supplied but the .descendants was not being recognised which may of been because I didn't have a reference to the namespace?
Sorry if I'm asking silly questions it's all stuff I haven't done much with.
Thanks again for your response
Paul
I just have to re-remember my webforms skills. Will be back with an expanded answer tomorrow.
Asbjørn
Hi Asbjorn,
No problems mate I am stopping for today and will check this out with a fresh head tomorrow 😄. I did stumble across this though
So to get started, you’ll need to make a reference to “umbraco.dll” in your Visual Studio project. When you use the ContentService through a User Control, it’s also a good idea to make a reference to “Umbraco.Core.dll”. This allows you to make your User Control inherit from “Umbraco.Web.UmbracoUserControl” instead of the normal “System.Web.UI.UserControl” and gives you super easy access to local Umbraco data as well as the ContentService.
Seems useful ;)
Cheers
Paul
Hi Asbjorn,
Been looking into this on the wya into work this morning and i am having a problem with references. I cant seem to compile my code because there is a problem with the follwoing line
I have tried referencing the following namespaces but not had any luck
it doesnt seem to like the bottom one it is giving the following error Error 1 A using namespace directive can only be applied to namespaces; 'Umbraco.Web.UI.Controls.UmbracoUserControl' is a type not a namespace
Cheers
Paul
Here is an example usercontrol (only the cs file, the ascx file doesn't need changing, if I remeber correctly):
You'll need references to umbraco.dll, Umbraco.Core.dll and Umbraco.Web.UI.dll.
Asbjørn
Hi mate,
Just wanted to thank you for your help on this one i have managed to populate my ddl with the values of my vehicles :) using the code and info that you have supplied.
I think the issue that i was having was some missing references and some issues in my web config but a bit of juggling around and using a backed up version of my web config i was able to solve it.
For others who may be interested in this here is the code i used, i decided to do it by doc type instead of level. (hopefully its coded ok)
Couldnt of done this without your help mate so thank you very much for that
Paul
Thanks! Great you got working :) And I got to dust off my webforms knowledge...
Asbjørn
is working on a reply...