API Questions on Kentico API.
Version 6.x > API > How to count child node of a doc type View modes: 
User avatar
Member
Member
tony@3t - 1/11/2012 10:24:39 AM
   
How to count child node of a doc type
Hello,

I have a doc type page with several sub pages. I want to count the total number of sub page and display the number in the transformation of the page. Most of research results I found were required to write a customized function in the code behind. I wonder if there is method in transformation to do it.

Thanks!

Tony

User avatar
Member
Member
nicolas.juteau-nexun - 1/11/2012 1:30:32 PM
   
RE:How to count child node of a doc type
Hi Tony,

You can call customized functions from code-behind in transformations, it just requires you to create a static class and put it in App_Code folder

Then call your custom function using the following syntax in your transformation:

<%# YourNamespace.YourClass.YourMethod() %>

User avatar
Kentico Support
Kentico Support
kentico_radekm - 1/18/2012 1:44:36 AM
   
RE:How to count child node of a doc type
Hello.

I did not find such a method in our API, however you can use custom macro or custom function in transformation (both is described in our Developer's guide).

The method itself can do something similar to:

public int CountChildDocs()
{
System.Data.DataSet ds = null;

// create a TreeProvider instance
UserInfo ui = UserInfoProvider.GetUserInfo(CMS.CMSHelper.CMSContext.CurrentUser.UserName);
CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);

// get dataset of tree nodes specified by alias path and class names (separated by semicolon).

ds = tree.SelectNodes("CorporateSite", CMS.CMSHelper.CMSContext.CurrentAliasPath + "/%", "en-us", true, "cms.menuitem");

return ds.Tables[0].Columns.Count;
}


You can find more info about SelectNodes() method and its parameters in our API reference file.

Best Regards,
Radek Macalik