Macro to count child pages of specified page

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?

3 answers

1 accepted

Accepted Answer
2 votes

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!

That's perfect, thank you Amalia!

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>

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

Hi Amalia

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

Cool, but it isn't recursive.

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?

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

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.

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

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()

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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Confluence

What are your project planning tips?

Hello Community,  Jessica here from the Confluence product marketing team! Today I wanted to get your takes on project planning –– what works, what doesn’t, how do you know if you’re doing it r...

369 views 2 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you