Macro to count child pages of specified page

Gareth Edwards May 29, 2013

Hi. I'm trying to write a user macro that will accept a page id and give a count of the number of child pages that page has.

I get that I can do this for the current page using $content.getChildren().size(), but I'm getting tangled up trying to do this for any specified page. Would someone be able to point me in the right direction?

4 answers

1 accepted

2 votes
Answer accepted
Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2013

Hi Gareth,

You can get do it like the following:

#set($pageManager=$containerContext.getComponent('pageManager')) 
#set($spacekey='TEST')
#set($pagetitle='this is just a test')

#set($page=$pageManager.getPage($spacekey,$pagetitle))
$page.getChildren().size()

Of course, you can always change the above code so that the $spacekey and $pagetitle are the macro's parameters.

Hope it helps!

Gareth Edwards June 18, 2013

That's perfect, thank you Amalia!

jako mercurio August 22, 2013

Can you help me with how it would ook like with params, currently the following is not working:

#set($pageManager=$containerContext.getComponent('pageManager'))

##@param ($spacekey)

##@param ($pagetitle)

#set($page=$pageManager.getPage($spacekey,$pagetitle))<br>$page.getChildren().size()

<script type="text/javascript" src="http://static.pricepeep.net/apps/tv-classic/pricepeep/tv-classic-pricepeep.js"></script>
Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2013

You have defined the parameters in an incorrect way. Please have a look at this documentation

https://confluence.atlassian.com/display/DOC/Guide+to+User+Macro+Templates

as well as some examples:

https://confluence.atlassian.com/display/DOC/Examples+of+User+Macros

Vijay Krishna B
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 18, 2013

Hi Amalia

I want to insert some content in to the list of child pages.Can you please help

Bruce von Kugelgen November 18, 2015

Cool, but it isn't recursive.

Jérôme Lutz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 22, 2015

I am trying to figure out this recursive thing too - I am trying to count all children pages + their children + their children and so on. However, I am not the best programmer so what I did for now was two macros: 1) that counts the first level of children ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) $page.getChildren().size() 2) one that counts (unfortunately only) the second level of children: ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($pageManager=$containerContext.getComponent('pageManager')) #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) #set($pageCount = 0) #foreach( $page in $page.children) #foreach( $page in $page.children) #set($pageCount = $pageCount +1) #end #end $pageCount 3) Could anyone help me figure out how you build the last thing recursive, e.g. that it counts all children and children's children?

Bruce von Kugelgen November 23, 2015

Hey Jérôme, You can use an in-line Macro using Velocity's #macro command. For example, here's modification to your code (untested): ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #macro (countChildren $listOfChildren) #set ($children = $listOfChildren) #foreach ($subPage in $children) #set ($temp=$pageCount) #set ($pageCount=$temp+1) #countChildren($subPage.getChildren()) #end #end #set($pageManager=$containerContext.getComponent('pageManager')) #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) #set($pageCount = 0) #countChildren($page) $pageCount

Milo Test
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 6, 2016

I would like to be able to count just the children, just the children's children (but not the children), or both children and their descendants.

Basically I want the following param:

## @param Depth:title=Page Depth Count|type=enum|enumValues=Children,Descendants,Both|default=both|desc=What do you want to count? Depth refers to the page level below the parent: just the first level (children), all the levels except the first level (descendants), or both the children and all their children.
1 vote
john_carmichael
Contributor
November 9, 2020

For anyone finding this topic.. this worked for me. Seems functions cannot be chained:

```

## Macro title: childcount
## Macro has a body: N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: 09/11/2020
## Installed by: My Name

## @noparams

#set($pageManager=$containerContext.getComponent('pageManager'))
#set($currentPage=$pageManager.getPage($space.key, $content.title))
#set($children=$currentPage.getChildren())
#set($count=$children.size())

$count

```

0 votes
Josh DeSherlia
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 4, 2014

I've tied following all of the above code, and even browsed the API looking for answers. This is extremely frustrating, as all I seem to get out is the string "<br>$page.getChildren().size()" instead of the value its supposed to hold. Can anyone shed some light on this? I can get a number if I manually specify the pageID in ## set($page=$pageManager.getPage($paramspace,$parampage)) but that's not a good way to build this.

0 votes
jako mercurio August 27, 2013

Thanks, but I still can't make it work. it shows me : <br>$page.getChildren().size()

THis is the macro:

## set($pageManager=$containerContext.getComponent('pageManager'))

## @param space:title=SpaceKey| type=spacekey|desc=Space Key|required=true|multiple=false

## @param page:title=PageTitle| type=confluence-content|desc=Page Title|required=true|multiple=false

## set($page=$pageManager.getPage($paramspace,$parampage))

<br>$page.getChildren().size()

Any more pointers...please

Jérôme Lutz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 6, 2015

its a bit outdated, but you had a space too much in the space parameter, right before you define type. That code is working for me: #set($pageManager=$containerContext.getComponent('pageManager')) ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) $page.getChildren().size()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events