joym8
joym8

Reputation: 4212

Not able to get property of page

We have Kentico 11.0.26 with MVC

I have a page that I can retrieve like this

General mainPage = GeneralProvider.GetGeneral(completeAlias, "en-us", SiteContext.CurrentSiteName)
.Columns("DocumentName", "NodeId", "NodeParentID", "NodeLevel");

Now I need to get the sibling pages of this node that may or may not be of the same type.

To preserve the node order I'm trying to get them in one call:

var siblings = DocumentHelper
    .GetDocuments()
    .OnSite("mySite")
    .Culture("en-US")
    .Where(d => d.NodeParentID == mainPage.NodeParentID && d.NodeLevel == mainPage.NodeLevel)
    .OrderBy(d => d.NodeOrder)
    .ToList();

But I cannot retrieve a field value that exists in only one of the page types. The following throws null reference exception:

foreach (var item in siblings)
{
    string colValue = item.GetValue("myColumn").ToString();
}

How can I retrieve this field value that exists in only one of the page types (not every sibling may have this column)?

Upvotes: 0

Views: 172

Answers (2)

joym8
joym8

Reputation: 4212

It's important to use both .Types() and .WithCoupledColumns() in the document query to retrieve column values of multiple page types.

https://docs.kentico.com/k11/custom-development/working-with-pages-in-the-api

var siblings = DocumentHelper
                .GetDocuments()
                .Types("MySite.General","MySite.Link")
                .WithCoupledColumns()
                .OnSite("MySite")
                .Culture("en-US")
                .Published();

Upvotes: 1

Peter Mogilnitski
Peter Mogilnitski

Reputation: 998

You can use .TryGetValue instead of GetValue

var colValue;
item.TryGetValue("myColumn", out colValue);

or check classname before and then get value i.e. item.GetValue("ClassName")

Upvotes: 0

Related Questions