Reputation: 4212
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
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
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