Child Node Count in Transformation

Katrina Miday asked on May 19, 2015 22:50

I have a transformation that would display the number of child pages within a folder. I was using NodeChildNodesCount to display the number of children pages. With v8.1 and up, that column was removed. I've been trying to use TreeNode.Children.Count in various ways with no luck.

I'm sure I'm missing something simple, anyone have a solution to display the number of children pages? Thanks in advance!

Correct Answer

Charles Matvchuk answered on May 20, 2015 00:55

@Katrina, I have a helper function that you can use, put it your custom functions and then you can call it in any transformations by CustomFunctions.ChildCount(nodeGuid)

public static string ChildCount(string nodeGuid)
    {
        string childCount = "0";
        if(!string.IsNullOrEmpty(nodeGuid))
        {

            Guid nodeGUID = new Guid(nodeGuid);
            if(nodeGUID != null)
            {
                int nodeId = TreePathUtils.GetNodeIdByNodeGUID(nodeGUID, SiteContext.CurrentSiteName);

                if (nodeId > 0)
                {
                   TreeProvider tp = new TreeProvider(SiteContext.CurrentUser);
                    TreeNode node = tp.SelectSingleNode(nodeId);
                    childCount = node.Children.Count().ToString();
                }

                return childCount;
            }
            return childCount;
        }
        return childCount;
    }
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on May 19, 2015 23:41

You're correct Katrina, that did change in 8.1. See the article and what to use instead.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 00:57 (last edited on May 20, 2015 00:59)

Note: You would replace "CustomFunctions" with the name of your custom functions class. Also this returns count as a string, you could have it return as an int or Convert the string to an int in the transformation. You will have to pass it the nodeGuid. Since you can call this anywhere you can get the count of child pages for any page in the system from anywhere.

Basically you will create a new transformation method for yourself.

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 18:56

This code does not work. It always returns zero for the child count

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 8, 2017 18:59

@jason what isn't working? Are you getting an error? What version are you using? Maybe post a new question since this one is over 2 years old.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on August 8, 2017 19:00

Jason, I am using the code in production systems and just retested it. It works correctly. I am unsure how you are implementing it. Feel free to share more.

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:12

Here is the code I have created in the App_Code folder. When i call the function from an item transformation it always returns zero.

using System; using System.Reflection; using CMS.Base; using CMS.Core; using CMS.DataEngine; using CMS.SiteProvider; using CMS.DocumentEngine;

public class CustomFunctions
{

    public static int ChildCount(string nodeGuid)
    {
        int childCount = 0;
    int nodeId = 0;
        if (!string.IsNullOrEmpty(nodeGuid))
        {

            Guid nodeGUID = new Guid(nodeGuid);
            if (nodeGUID != null)
            {
                nodeId = TreePathUtils.GetNodeIdByNodeGUID(nodeGUID, SiteContext.CurrentSiteName);

                if (nodeId > 0)
                {
                    TreeProvider tp = new TreeProvider(CMS.Base.CMSActionContext.CurrentUser);
                    TreeNode node = tp.SelectSingleNode(nodeId);
                    childCount = node.Children.Count;
                }

                return childCount;
            }
    else 
    {
        childCount = 0;
    }
            return childCount;
        }
        return childCount;
    }

}
0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on August 8, 2017 19:18

Start debugging in Visual Studio and put in breakpoint(s) and step to find out where it is returning.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on August 8, 2017 19:19

Also, please show your call from your transformation.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 8, 2017 19:22 (last edited on August 8, 2017 19:23)

in your transofrmation you need to call it like so:
CustomFunctions.ChildCount(Eval<string>("NodeGuid"))

Problem with the answer and code is it will count all children and not take into consideration their page type, if they are published or any other "where" conditions.

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:22

This is where it is returning zero.

childCount = node.Children.Count;

I already changed my transformation call. Perhaps you can share your call here and i can try it.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on August 8, 2017 19:24

I also see you didn't use my code exactly as I wrote it. When you instantiate your TreeProvider you are using CMSActionContext, please use SiteContext. Also node.children.count is a method.

Please utilize my code exactly, if not or you have decided to make modifications, then I am unsure what issues you may incur.

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:29 (last edited on August 8, 2017 19:32)

When I used your code exactly as-is, there were two errors.

  1. 'SiteContext' does not contain a definition for 'CurrentUser'

  2. Non-invocable member 'InfoObjectCollection TreeNode.Count' cannot be used like a method.

My transformation call was:

CustomFunctions.ChildCount(Eval("DocumentGUID").ToString())

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 8, 2017 19:31

You need to use MembershipContext.AuthenticatedUser

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:31 (last edited on August 8, 2017 19:32)

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:35

Ok i will change the line of code to this.

TreeProvider tp = new TreeProvider(CMS.Membership.MembershipContext.AuthenticatedUser)

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on August 8, 2017 19:38

Of course. The exercise was to count child nodes regardless of page type, published status and/or where clause.

0 votesVote for this answer Mark as a Correct answer

Jason Garland answered on August 8, 2017 19:40

Ok thanks for the help. the only remaining issue is this:

Non-invocable member 'InfoObjectCollection TreeNode.Count' cannot be used like a method.

0 votesVote for this answer Mark as a Correct answer

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