Get First Child Name By Parent Path?

Greg Laycock asked on March 8, 2017 18:12

I've got what I think is a simple request, but I'm so far unable to figure it out. I have a path to a document (ex: "/Product/Category") and I'd like to, via the code-behind, get the document name of the first child underneath that path. How do I do this?

Kentico 10.0 ASPX Templates + Portal

Recent Answers


Jan Hermann answered on March 8, 2017 18:19

.CS:

TreeNode tn = ...
string name = tn.Parent.Children.FirstItem.DocumentName;

macro:

name = Documents["/Product/Category"].Parent.Children.FirstItem.DocumentName;

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on March 8, 2017 19:14

I need to use this in a code-behind, so the macro is right out. When I use your c# code, I get the following error:

Cannot implicitly convert type 'string' to 'CMS.DocumentEngine.TreeNode'

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on March 8, 2017 19:27

I was able to take a different approach. Instead of using the path, I referenced the particular page type (all pages of which exist under the given path) as follows:

DocumentHelper.GetDocuments([page type]).OrderBy("NodeOrder").FirstObject.NodeAlias;

0 votesVote for this answer Mark as a Correct answer

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