Umbraco will always load from the cache if it is enabled, unless you retrieve something that it doesn't have in the cache. The simplest way is using the now in-built uQuery library:
foreach (var page in uComponents.Core.uQuery.GetRootNode().Children)
{
}
I should have mentioned I tried uQuery code similar to what you have suggested. However when viewed in visual studio through the debugger it only seems to return the actaul root node although ,like you, I thought it would have itterated through all the children. As a side point you mention
uComponents.Core.uQuery but in my code uquery is in the umbraco namspace umbraco.uQuery dont know if this is relavent.
Im just wondering if it is maybe not possible to get to the cache from a user control maybe my only option is to use the content service to directly read from database?
My apologies - it seems I misunderstood the problem. You're saying that you only want to hit the cache instead of the database? You should use the NodeFactory as it is read-only and super fast.
In one of the sites I recently worked on, I added custom caching to cache my own business objects. I was parsing the DynamicNode into my POCO's and then caching them using the techniques mentioned here which is standard .NET caching.
Node myNode = new Node(-1); //root node var pages= myNode.GetChildNodes();
int index = 0; foreach(var page in pages) { index++; }
again from within debugger pages only seems to contain the root node so the loop only itterates once. I am not sure what is causing this. maybe the umbraco cache document cant be read from a user control?
This should not be the case - you should still be able to access the cache through the NodeFactory - no matter where it is called from. I've called it from other class libraries and it works fine.
Can you please check if you have an umbraco.config file in your App_Data folder and that it contains all your nodes from your website, please? Also try republishing everything from the root node in the Content section of Umbraco to regenerate it.
umbraco.config is there. Also republished everything from the root node in the Content section of Umbraco as well. But still no change.
what I noticed though is that if I expand the page variable from the above loop in the debugger as I mentioned it only returns the root node but its children can be expanded and all the child nodes I expect to get are there, very strange.
Just tried it from a razor script rendered from a content page and got same behaviour so nothing to do with fact im using user control.
Tested code but instead of -1 for root node I passed in the id of another content node and it worked as expected. ie foreach loop looped through returned children. so something to do with fact it is root node I am using.
Node myNode = new Node(2169);
int index = 0; foreach (var page in myNode.Children) { index++; }
UPDATE: got a work around
var root = umbraco.uQuery.GetRootNode();
var children = root.GetDescendantNodes().Where(x => x.Level == 2);
this returns all pages directly under root. It seems to be that if you return the root node (either using uquery or nodefactory) you can not directly itterate offer the children using the children porerty or GetChildNodes() method, dont know why this is.
Chris if you get a chance could you test this out on one of your sites to see if this is something to do with my setup. ive tested iton my setup on a couple of sites and I always get the same results.
I've had no issues with the following code, which I added to the start of one of my Razor macros.
var root = uQuery.GetRootNode();
var pages = root.GetDescendantNodes();
foreach (Node page in pages)
{
Response.Write(page.Name);
if (page != pages.Last()){Response.Write(", "); }
}
What is the root structure of your Content tree? Do you have one child node below the Content node or several? If you have more than one, try having all your site starting from a single node. The issue you describe can sometimes happen when you have more than one node directly below the Content node. Glad you came up with a solution, and if it works then hey... but it should really work with all descendants of your root node. Without seeing the CMS directly though I am only able to guess at the actual problem I'm afraid.
The code you tried works with mines too but try replacing GetDescendantNodes() with GetChildNodes() and see if that works. It works with every node but the root one.
Hi Les, Yes I get the same result but I believe GetChildNodes() is only meant to give the immediate children on the level below the current node. This is why it is used in the example Razor sitemap example. See this example for more
I tested the code above - but replacing GetDescendantNodes() with GetChildNodes() - and it showed me the 2 pages I have directly below the root level - just as expected.
Are you saying that when you try it you get nothing? That is strange indeed. The only thing I could suggest is creating a second test website, using the sample content option when installing, and test the same code on that. Could be that you lost something due to custom code.
If it still doesn't work, try putting .Where(node => node.Visible) on the end. Check that all nodes are published and are visible from the browser. If all else fails, hard coding the node ID is not the end of the world. Stick it in the web.config and forget about it.
getting list of content nodes below root node from within .net user control
how can I get a list of content nodes directly below root node from within .net user control.
Preferably from the cached nodes and no direct from database.
Hi
Umbraco will always load from the cache if it is enabled, unless you retrieve something that it doesn't have in the cache. The simplest way is using the now in-built uQuery library:
Cheers,
Chris Randle
Hi Chris
I should have mentioned I tried uQuery code similar to what you have suggested. However when viewed in visual studio through the debugger it only seems to return the actaul root node although ,like you, I thought it would have itterated through all the children. As a side point you mention
Hi LJ,
My apologies - it seems I misunderstood the problem. You're saying that you only want to hit the cache instead of the database? You should use the NodeFactory as it is read-only and super fast.
In one of the sites I recently worked on, I added custom caching to cache my own business objects. I was parsing the DynamicNode into my POCO's and then caching them using the techniques mentioned here which is standard .NET caching.
Let me know if this resolves the problem :-)
- Chris
HI chris,
I tried the following:
Node myNode = new Node(-1); //root node
var pages= myNode.GetChildNodes();
int index = 0;
foreach(var page in pages)
{
index++;
}
again from within debugger pages only seems to contain the root node so the loop only itterates once. I am not sure what is causing this. maybe the umbraco cache document cant be read from a user control?
Hi LJ
This should not be the case - you should still be able to access the cache through the NodeFactory - no matter where it is called from. I've called it from other class libraries and it works fine.
Can you please check if you have an umbraco.config file in your App_Data folder and that it contains all your nodes from your website, please? Also try republishing everything from the root node in the Content section of Umbraco to regenerate it.
-Chris
Hi Chris,
umbraco.config is there. Also republished everything from the root node in the Content section of Umbraco as well. But still no change.
what I noticed though is that if I expand the page variable from the above loop in the debugger as I mentioned it only returns the root node but its children can be expanded and all the child nodes I expect to get are there, very strange.
Just tried it from a razor script rendered from a content page and got same behaviour so nothing to do with fact im using user control.
Tested code but instead of -1 for root node I passed in the id of another content node and it worked as expected. ie foreach loop looped through returned children. so something to do with fact it is root node I am using.
Node myNode = new Node(2169);
int index = 0;
foreach (var page in myNode.Children)
{
index++;
}
UPDATE: got a work around
var root = umbraco.uQuery.GetRootNode();
var children = root.GetDescendantNodes().Where(x => x.Level == 2);
this returns all pages directly under root. It seems to be that if you return the root node (either using uquery or nodefactory) you can not directly itterate offer the children using the children porerty or GetChildNodes() method, dont know why this is.
Chris if you get a chance could you test this out on one of your sites to see if this is something to do with my setup. ive tested iton my setup on a couple of sites and I always get the same results.
Cheers les
Hi Les,
I've had no issues with the following code, which I added to the start of one of my Razor macros.
What is the root structure of your Content tree? Do you have one child node below the Content node or several? If you have more than one, try having all your site starting from a single node. The issue you describe can sometimes happen when you have more than one node directly below the Content node. Glad you came up with a solution, and if it works then hey... but it should really work with all descendants of your root node. Without seeing the CMS directly though I am only able to guess at the actual problem I'm afraid.
Hi Chris,
The code you tried works with mines too but try replacing GetDescendantNodes() with GetChildNodes() and see if that works. It works with every node but the root one.
Hi Les, Yes I get the same result but I believe GetChildNodes() is only meant to give the immediate children on the level below the current node. This is why it is used in the example Razor sitemap example. See this example for more
I tested the code above - but replacing GetDescendantNodes() with GetChildNodes() - and it showed me the 2 pages I have directly below the root level - just as expected.
Are you saying that when you try it you get nothing? That is strange indeed. The only thing I could suggest is creating a second test website, using the sample content option when installing, and test the same code on that. Could be that you lost something due to custom code.
If it still doesn't work, try putting .Where(node => node.Visible) on the end. Check that all nodes are published and are visible from the browser. If all else fails, hard coding the node ID is not the end of the world. Stick it in the web.config and forget about it.
is working on a reply...