DocumentHelper Query not returning DocumentAttachment

Stuart1 Freeman1 asked on December 20, 2019 19:32

Hi All,

I have the following query:

DocumentHelper.GetDocuments()
    .Type("MyCustom.Menu", q => q
        .Columns("TopMenuItemsID", "MenuName"))
    .Type("MyCustom.MenuItem", q => q
        .Columns("MenuItemID", "MenuItemText", "MenuItemPage", "MenuItemIcon", "ExternalUrl"))
    .OnSite(SiteContext.CurrentSiteName)
    .Path("/My-header-menu")
    .OrderBy("NodeLevel", "NodeOrder", "NodeName")
    .Published();

I use the results, grabbing the first document with .FirstOrDefault(), which is my menu root, I loop through the items and their children using a recursive function.

when I attempt to:

var menuItem = new MenuItemViewModel
                        {
                            MenuItemText = childNode.GetValue("MenuItemText", "").ToString(),
                            MenuItemRelativeUrl = childNode.GetValue("MenuItemRelativeUrl", "").ToString(),
                            MenuItemIcon = childNode.GetValue<DocumentAttachment>("MenuItemIcon", null),
                            ExternalUrl = childNode.GetValue("ExternalUrl", "").ToString()
                        };

The MenuItemIcon is always null even when the MenuItem has a DocumentAttachment defined.

Correct Answer

Roman Hutnyk answered on December 21, 2019 11:38

I believe "MenuItemIcon" is just a link to the DocumentAttachment which lives in a separate table.

You're trying to cast Attachment ID or GUID to an actual DocumentAttachment, which obviously won't work.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Stuart1 Freeman1 answered on December 23, 2019 15:53

Hi Roman you are right, that was indeed what was happening. I am new to Kentico and from the generated classes (model and provider) it wasn't apparent this was happening. In the model there was is a property that is a DocumentAttachment but in the constructor it's a GUID.

0 votesVote for this answer Mark as a Correct answer

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