Get value from TreeNode

Chibin Zhang asked on September 13, 2019 10:30

Hi all,

I'd like to get a field out of a tree node. The TreeNode.GetValue (as well as the TreeNode.TryGetValue) does not work. The kentico version is 12sp which is 12.0.32.

Here are the details.

I have a page type called BaseType, with one Text field TestField in it. Then I have a page type called Home, with a Text field HomeHeader in it.

Now, in the code, I'm not using the HomeProvider generated by kentico because there could by other page types that inheriting from BaseType, so I'd like to use the TreeNode type for all the type to get the TestField value.

In the code: var homeNode = treeProvider.SelectNodes().Path("/home").OnCurrentSite().Culture(lang).TopN(1).FirstOrDefault(); var fieldValue = ValidationHelper.GetString(homeNode.GetValue("TestField"), "asdf");

The homeNode.GetValue part do not work. It always return null.

Additionally, I've also tried homeNode.TryGetValue("TestField", out var result). The interest thing is this method returns true, but the result is null. This I believe is incorrect because similar to the TryParse patter (search for the key workd "TryParse"), it should "defines a corresponding TryParse method which attempts to parse, but returns false if parsing is unsuccessful and returns the result of a successful parsing using an out parameter."

If I use the generated HomeProvider, everything works.

Is this a bug? Or am I using it wrong?

Thanks.

Correct Answer

Dragoljub Ilic answered on September 16, 2019 09:58

Hi Chibin,

Data in Kentico for page types is stored in two separate table. CMS_Document and the second one is the specific for page type. Connection between those two is [DocumentForeignKeyValue] column in CMS_Document table which present FK to custom table. Simply said, you should look at Kentico API as an complex SQL query builder where you specify bunch of properties and conditions. TreeProvider or DocumentHelper does not know to which custom table (based on page type) FK lead to, so you need to specify it, which will actually do a JOIN to another table (based on the page type name) and because of that you will be able to retrieve custom fields.

Hope this will shed some light on Kentico API.

Best regards, Dragoljub

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dragoljub Ilic answered on September 13, 2019 12:24 (last edited on September 13, 2019 12:25)

Hi,

Problem is that you can't get custom field value from tree node object unless you specify types (class name). It's working well when you use HomeProvider because it's strongly typed. To make your code works with tree provider, you must specify types of the nodes that you wanna get, something like this:

var homeNode = treeProvider.SelectNodes().Path("/home").OnCurrentSite().Type("custom.HomePage").Culture(lang).TopN(1).FirstOrDefault();

If you want to get custom fields of several page types, you can specify all them by using Types() extensions like this: treeProvider.SelectNodes().Path("/home").Types("custom.HomePage", "custom.AboutUs")

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Chibin Zhang answered on September 16, 2019 08:43

Hi Dragoljub,

Thanks for the reply.

From your response it looks like the Kentico api is designed in a weird way that in the code the class name needs to be pass in to get custom fields. The reason I say it's weird is that for each node it should already know it's class name. Every page knows its page type, and every page type knows its Code name which is defined in the page type form. The CLASS_NAME property in the generated code is clearly the same value as the Code name value in a page type form. Then why the api is designed to pass in a magic string to get the custom fields if the node already knows it's type?

Also, even if the custom field does not work without the class type, wouldn't the TryGetValue methods return false instead of return true but out a null value which obviously will cause a null exception?

Thanks!

0 votesVote for this answer Mark as a Correct answer

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