Get the children of the current document of a certain type?

Simon Goldsmith asked on June 13, 2017 15:16

I am using the following code to get the children of the current document("casestudy" pagetype) which is then binded to a repeater.

var CaseStudyQuotes = CurrentDocument.Children;
QuotesRepeater.DataSource = CaseStudyQuotes;
QuotesRepeater.DataBind();

The problem I have is I also ONLY want to get certain child Page Types("cms.CaseStudyQuotes"), and if I try to use

<%#Eval("Quote")%>

in the repeater, it tells me the "DataBinding: 'CMS.DocumentEngine.TreeNode' does not contain a property with the name 'Quote'."?

Do I need to use a literal in my repeater and OnItemDataBond to set the literal to the Quote value?

Many thanks Simon

Recent Answers


Peter Mogilnitski answered on June 13, 2017 15:53 (last edited on June 13, 2017 15:54)

Take a look at Document Query API. It should be something like:

  var CaseStudyQuotes = DocumentHelper.GetDocuments("cms.CaseStudyQuotes")
            .WhereEquals("NodeParentID", CurrentDocument.NodeID)
            .Columns("Quote, DocumentCulture, DocumentName")
            .OrderBy("DocumentName")
            .Culture("en-US")
            .Published()
            .CombineWithDefaultCulture();
2 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on June 13, 2017 15:55

There are many ways to get there:

CurrentDocument.Children.Where(x => x.ClassName == "cms.CaseStudyQuotes")

or

DocumentHelper.GetDocuments("cms.CaseStudyQuotes")
.WhereEquals("NodeParentID", CurrentDocument.NodeID);

Second options gives you a possibility to filter out not published pages, or check security and many more.

1 votesVote for this answer Mark as a Correct answer

Simon Goldsmith answered on June 13, 2017 17:31

Thanks for your offerings guys I will have a look. :)

0 votesVote for this answer Mark as a Correct answer

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