Retrieve children field values - MVC

Sylvain C. asked on September 19, 2019 21:56

I have the following hierarchy:

Event 1
--Meeting 1
--Meeting 2
....

I am doing the following for extracting the events:

var test = EventProvider.GetEvents()
                            .AddColumns("EventTitle")  
                            .Select(m => new Event()
                            {                                
                                Name = m.EventTitle                                
                            })
                            .ToList();

But now I would like to return the list of events with their nested children meetings. I tried to obtain the title of my meetings (available in the MeetingTitle field for this pagetype: meeting 1" or "meeting 2" in my example) but it doesn't return anything:

var test = EventProvider.GetEvents()
                .AddColumns("EventTitle")  
                .Select(m => new Event()
                {                                
                    Name = m.EventTitle,    
                    MeetingList = m.Children
                        .Select(u => new Meeting
                        {
                            Name = u.GetValue("MeetingTitle")
                        })
                })
                .ToList();

What is the correct way for extracting the field values of my children's nodes?

Thank you

Sylvain

Correct Answer

Eric Dugre answered on September 20, 2019 17:54

Hello Sylian,

Can you please try the .Children call with WithAllData? For example:

var menuitems = MenuItemProvider.GetMenuItems()
        .WhereEquals("NodeAliasPath", "/Events")
        .TypedResult
        .Select(m => new
        {
            Name = m.MenuItemName,
            Events = m.Children.WithAllData.Select(ev => new
            {
                Start = ev.GetValue("EventDate")
            })
        });
0 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on September 20, 2019 19:01 (last edited on September 20, 2019 19:02)

I'd say you need to use document query, let say you have couple page types and you want to get subsection of the document tree (here is the pic):

public class KenticoDocument
{
    public int NodeId { get; set; }
    public int NodeParentId { get; set; }
}

public class Event : KenticoDocument
{
    public string EventTitle { get; set; }
    public List<Meeting> MeetingList { get; set; }
}

public class Meeting : KenticoDocument
{
    public string MeetingTitle { get; set; }
}

Get your subtree as List (i.e. list of DataRow form DB)

var section = DocumentHelper.GetDocuments()
                .Types(new string[] { "CMS.MenuItem", "CMS.BookingEvent" })
                .Path("/Community/Events/", PathTypeEnum.Section)
                .OnSite("CorporateSite")
                .Culture("en-us")
                .Result.Tables[0].AsEnumerable().ToList();

and build your hierachy

var hierarchy = section.Where(s => s.Field<string>("ClassName") == "CMS.MenuItem")
        .Select(x =>
            new Event
            {
                EventTitle = x.Field<string>("DocumentName"),
                NodeId = x.Field<int>("NodeID"),
                NodeParentId = 0,
                MeetingList = section
                    .Where(s => s.Field<string>("ClassName") == "CMS.BookingEvent" && s.Field<int>("NodeParentID") == x.Field<int>("NodeID"))
                    .Select(
                            m => new Meeting
                            {
                                MeetingTitle = m.Field<string>("DocumentName"),
                                NodeId = x.Field<int>("NodeID"),
                                NodeParentId = x.Field<int>("NodeParentID"),
                            })
                     .ToList()
            });
0 votesVote for this answer Mark as a Correct answer

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