Use Custom Field with TreeNode

David Waters asked on March 14, 2022 18:39

The Modal code with the query using the multidocumentquery

 public List<TreeNode> GeneralList { get; set; }

// General
             GeneralList = new MultiDocumentQuery()
                .Type("Site.Article", q => q
                    .Columns("ArticleID", "ArticleTitle", "Image")
                    .Path("%/Articles/%", PathTypeEnum.Explicit)
                    .Path("%/Posts/%", PathTypeEnum.Explicit)
                .Type("Site.MarketingPage", q => q
                    .Columns("MarketingPageID", "Title", "Hero")
                    .Path("%/Showcase-Pages/%", PathTypeEnum.Explicit)
                .Where("DocumentID IN (SELECT[DocumentID] FROM CMS_DocumentCategory WHERE[CategoryID] = 46 GROUP BY DocumentID HAVING COUNT('DocumentID') = 1)")
                .Columns("ArticleTitle", "Title", "Hero", "Image", "ArticleID", "MarketingPageID", "DocumentID", "NodeName", "NodeAliasPath", "DocumentPublishFrom", "NodeAlias", "ClassName")

The query turns out fine, grabbing all the info needed but when I go into the view:

    @foreach (var page in Model.GeneralList)
                            <div class="sponsored-article container">
                                <a href="@page.AbsoluteURL">
                                    <img src="" width="435px" height="245px" />

NodeName pulls in fine but if I use @page.ArticleTitle to try and get the article title from the Article column, I get the error: CS1061: 'TreeNode' does not contain a definition for 'ArticleTitle' and no accessible extension method 'ArticleTitle' accepting a first argument of type 'TreeNode' could be found (are you missing a using directive or an assembly reference?)

Any thoughts on how to implement it so these custom fields will pull in like the default ones?

Thanks for any help.

Correct Answer

Arjan van Hugten answered on March 15, 2022 09:30

You can use the TreeNode.GetStringValue method to retrieve specific page type column values.

page.GetStringValue("ArticleTitle", page.NodeName)

1 votesVote for this answer Unmark Correct answer

Recent Answers

David Waters answered on March 15, 2022 15:35

Awesome, that and GetValue seem to be working, I'll continue to mess with them. Thanks for the help Arjan.

0 votesVote for this answer Mark as a Correct answer

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