How to get an array of document properties from a TreeNode?

Charles Wesley asked on March 19, 2014 13:03

Is it possible to get an array/list of document properties from a TreeNode object?

My use case is I would like to write a class that wraps a custom Document Type. This class will have a method that outputs a formatted string that includes values entered by the user in the CMSDesk.

If I know the name of the property in advance I can do the following:

(TreeNode)node.GetValue("Key");

However, because properties could be added or removed from the Document Type in the CMS Site Manager, I don't want to hard code any strings in the class.

Instead, I'd like to access them kind of like this:

string[] keys = (TreeNode)node.GetKeys();
Dictionary<string, string> dictionary = new Dictionary<string, string>();

foreach(string key in keys)
{
  dictionary.Add(key, node.GetValue(key));
}

I don't see a method on the TreeNode object that would give me the data I'm looking for--is there a way to do this?

Correct Answer

Brenden Kehren answered on March 19, 2014 14:57

Are you talking about document type properties? Or do you want all the tree node information as well? Here is an awesome article on how to create strongly typed doc types. I've also used some Linq to XML to get the properties for a particular class. Take a look at this old forum post.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Charles Wesley answered on March 19, 2014 16:20

That is basically it--I'll utilize both referenced sources in my implementation, thanks much!

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 19, 2014 18:19

The second one where I provide some code samples with Linq to XML works awesome if you are importing data from an external system. Simply name your column names the same and everything maps across. If you add a new field to the external system, simply add it to the custom table and everything will automatically pick up. Pretty sweet setup really.

Good luck!

0 votesVote for this answer Mark as a Correct answer

Charles Wesley answered on March 20, 2014 16:09

I cross-posted to StackOverflow as well and there is an answer from Brian McKeiver that meets my requirements:

It might depend on if you are creating a new TreeNodeor retrieving it from the Content Tree.

If you are using SelectSingleNode or SelectNodes to populate your node object than node.ColumnNames will give you a list of all the columns/fields that come back with that node's class.

foreach (string column in node.ColumnNames) { //do something helpful with each one string value = ValidationHelper.GetString(node.GetValue(column), string.Empty); }

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 21, 2014 06:14

That's a good answer as well. The last example I provided works very well for custom tables or where you plan to import amounts of data on a regular basis. Also, the .ColumnNames property doesn't include the datatype or control type, whereas the XML definition does (for all class types; doc type, custom table, biz form, etc). In my case I needed to check the datatype and in one particular instance a control type.

0 votesVote for this answer Mark as a Correct answer

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