How to programmatically get list of children

Cesar Pinilla asked on May 2, 2014 15:48

Hello, I'm trying to programmatically find all the children of a specific page in my content tree.

I've used both DocumentContext.CurrentDocument.Children and DocumentContext.CurrentDocument.AllChildren but neither seems to work consistently. If i'm logged in to the admin area, it seems to work, but if I'm viewing the page publicly (without logging in) they always return empty.

Thanks, Cesar

Correct Answer

Richard Sustek answered on May 4, 2014 03:30

Hi,

If you want to know the "number" of children which will work for non-logged users as well you can also use the macro:

{%CurrentDocument.NodeChildNodesCount|(identity)GlobalAdministrator%}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Cesar Pinilla answered on May 2, 2014 18:43

I should add that all pages are published so it's not a publishing issue

0 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on May 3, 2014 07:57

You can use DocumentHelper.GetDocuments("CMS.MenuItem").Path(DocumentContext.CurrentDocument.NodeAliasPath, PathTypeEnum.Children)

0 votesVote for this answer Mark as a Correct answer

Cesar Pinilla answered on May 5, 2014 11:48

Thank you all this worked! What is the reason behind .Children and .AllChildren not working?

0 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on May 18, 2014 04:55

Hi,

The reason is that there is a permission check when accessing the child documents like this. It basically requests the given documents and then counts them, but this should not be possible because you could access also other properties which might be a security issue.

There is a web.config key which disables this security check, but its better to use the code above in this case.

Kind regards,

Richard Sustek

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.