Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > Accessing child nodes through a dataset View modes: 
User avatar
Member
Member
matt-4hilton - 11/11/2010 1:39:50 AM
   
Accessing child nodes through a dataset
Hi all,

I'm building a custom macro for a web part, under the "No data behavior" section/field. The goal of the macro is to look at the child nodes of the current document (if there are any child nodes), and to display text in the "no record found text" field," depending on the document type of the child nodes.

My problem is this: I can't for the life of me figure out how to extract the document types of the child nodes from a dataset. My thinking was that the child node info was somehow stored in the tables of the dataset, and that each child node was essentially a row within [dataset].tables[0]. But for some reason, there are no rows - even when a number of child nodes definitely exist under the parent node.

Please take a look at the following code. I'd appreciate it if you pointed me in the right direction.


public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
bool childNodeIsProduct = false;
CMS.CMSHelper.ContextResolver aTempObject = (CMS.CMSHelper.ContextResolver) sender;
CMS.TreeEngine.TreeNode currentNode = aTempObject.CurrentDocument;

DataSet db;
CMS.TreeEngine.TreeProvider theTree = new CMS.TreeEngine.TreeProvider();
db = theTree.SelectNodes("shuki", currentNode.NodeAliasPath + "/%", currentNode.DocumentCulture, false);

if (db.Tables.Count == 0)
childNodeIsProduct = true;
else
{

DataRowCollection rows = db.Tables[0].Rows;
foreach (DataRow row in rows)
{
//it never gets in here because for some bloody reason there are never
//any rows
}


}



Thank you for your help!

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 11/11/2010 7:43:39 AM
   
RE:Accessing child nodes through a dataset
Hi,

could you please try to use following code:


case "custom":
match = true;
DataSet ds = null;

// create a TreeProvider instance
UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);

// get dataset of tree nodes specified by alias path and class names (separated by semicolon),
// the second parameter says whether to return default culture documents if the required
// document language version is not available

ds = tree.SelectNodes("CorporateSite", CMSContext.CurrentDocument.NodeAliasPath +"/%", CMSContext.CurrentDocument.DocumentCulture, true, "");

if (ds.Tables.Count == 1)
{
DataRowCollection rows = ds.Tables[0].Rows;

foreach (DataRow row in rows)
{
string classid = row["NodeClassID"].ToString();

}

}

break;


Best regards,
Ivana Tomanickova

User avatar
Member
Member
matt-4hilton - 11/13/2010 7:27:48 AM
   
RE:Accessing child nodes through a dataset
Hi Ivana,

Thank you for your response.

Unfortunately, your code doesn't work. The reason is that the 'Rows' table (you'll see in the abbreviated code below) has a Count of 0, and therefore the 'rows' object equals null.

As a result, the program never gets into the foreach loop, even if there are definitely child nodes that exist.

The problematic code:

DataRowCollection rows = ds.Tables[0].Rows;

foreach (DataRow row in rows)
{
string classid = row["NodeClassID"].ToString();
}

User avatar
Member
Member
matt-4hilton - 11/13/2010 10:21:47 AM
   
RE:Accessing child nodes through a dataset
Hi Ivana,

Many apologies, your code actually works - mostly. I was making an error when calling the SelectNodes function by not providing the proper sitename as the first parameter, and THAT'S why I wasn't getting any rows.

But now I'm getting all the rows! And I can access exactly what I need simply by calling row["ClassDisplayName"] in the foreach loop.

Thank you!