API
Version 7.x > API > TreeNode.GetValue() always returning null View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
ianwright-netconstruct.co - 10/29/2013 11:38:04 AM
   
TreeNode.GetValue() always returning null
I have written a custom Query to retrieve child documents (Products) where the Categories obtain particular values driven from a filter. My Query as defined in Kentico is:
SELECT DISTINCT ##COLUMNS## FROM CMS_Category as C
INNER JOIN CMS_DocumentCategory as DC on DC.CategoryID = C.CategoryID
INNER JOIN View_CMS_Tree_Joined as D on DC.DocumentID = D.DocumentID
WHERE C.CategoryEnabled = 'true' AND D.NodeAliasPath LIKE '/%' ##WHERE##
Group By D.NodeAliasPath, D.DocumentName, D.DocumentUrlPath, D.SKUPrice, D.SKUImagePath

An example query that I've used for testing that illustrates the columns I'm using and an example WHERE condition:
SELECT D.DocumentName, D.DocumentUrlPath, D.SKUImagePath, D.SKUPrice, D.NodeAliasPath FROM CMS_Category as C
INNER JOIN CMS_DocumentCategory as DC on DC.CategoryID = C.CategoryID
INNER JOIN View_CMS_Tree_Joined as D on DC.DocumentID = D.DocumentID
WHERE C.CategoryEnabled = 'true' AND D.NodeAliasPath LIKE '/%'
--AND (C.CategoryParentID = '27' AND c.CategoryName IN ('ManufacturerA','ManufacturerB','ManufacturerC')) OR
-- (C.CategoryParentID = '39' AND C.CategoryName IN ('Brown','Orange','Red')) OR
-- (C.CategoryParentID = '43' AND C.CategoryName IN ('Large','Medium','Small')) OR 1 = 1
Group By D.NodeAliasPath, D.DocumentName, D.DocumentUrlPath, D.SKUPrice, D.SKUImagePath

The problem I have, is once I get my TreeNodes back, I seem unable to retrieve values from it. A snippet of code is shown below, where treeNodes is a List<TreeNode>.
TreeProvider treeProvider = new TreeProvider(CMSContext.CurrentUser);
TreeNodeDataSet dataSet = treeProvider.ExecuteQuery("CMS.Product.ProductsByCategory", null, whereCondition, null, -1, columns, 0, 0, ref totalRecords);

if (dataSet != null)
{
treeNodes = dataSet.AsEnumerable<TreeNode>().ToList();
}
CacheHelper.Add(string.Format("Products_{0}", whereCondition.GetHashCode()), treeNodes, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
}

// If we have some data then create some objects that we can return as JSON
if (treeNodes != null && treeNodes.Count > 0)
{
var nodes = treeNodes.Skip(startPoint)
.Take(count)
.Select(node => new
{
NodeAliasPath = node.NodeAliasPath,
ProductTitle = node.DocumentName,
Price = node.GetStringValue("SKUPrice", String.Empty),
ProductImagePath = node.GetStringValue("SKUImagePath", String.Empty),
ProductLogoImagePath = node.GetStringValue("LogoImagePath", String.Empty)
});

// Load actual data
result.Data = new { ProductItems = nodes, AllLoaded = (startPoint + count) >= treeNodes.Count };
}

My problem is that whenever I use node.GetStringValue() or node.GetValue() I always receive a null/my default value instead of the actual value. This is the same for the
LogoImagePath
and the
SKUPrice
even though SKUPrice is a built in field.

What might I be doing wrong?

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 11/7/2013 4:41:37 PM
   
RE:TreeNode.GetValue() always returning null
Hello,

I'm adding our current conclusion according to our conversation through other channels, so other can see it too. We are also considering adding a KB article on DevNet, based on this issue.

The API works consistently when the TreeNode is constructed from the minimum required set of data (required columns).
However, it is not explicitly mentioned in the API examples and docs, so it might not be as obvious.... but this aim is not so simple.

We have debugged the scenario in our testing setup, and it appears the problem is in some key information missing - specifically the ClassName and NodeSKUID columns.

If the ClassName is missing, the TreeNode constructor cannot identify which coupled table belongs to the document type, therefore the custom document columns (like the "LogoImagePath") won't be available.
Moreover, if also the NodeSKUID is missing, it cannot associate it with the proper SKU, returning the plain TreeNode structure, not the SKUTreeNode with SKU properties.

So, to achieve your aim with the original approach, it should be enough to add the Classname and NodeSKUID to the columns passed to the ExecuteQuery (without table naming), as well as adding them to the query itself - to the Group By part:
... D.ClassName, D.NodeSKUID

Another note is that this approach is generally suitable for read-only access, as trying to update or write partial data would require more precise initial selection of the data (set of columns, etc.).

Regards,
Zdenek

User avatar
Certified Developer v7
Certified  Developer v7
ianwright - 11/8/2013 2:34:49 AM
   
RE:TreeNode.GetValue() always returning null
Thanks Zdenek - I completely forgot about this otherwise would have done so myself.