How to count the number of child pages with a particular label?
Asked by David Dube:

I'm trying to use local-reporter to count the number of children with a particular label... I must not be using the collection supplier correctly, as I'm not seeing any text output.

```
{report-block}{local-reporter:value:label risk_open &gt; label:content}   {content-filter:@self|scope="My Parent Page" &gt; children} {local-reporter}{report-body}{report-info:@self &gt; collection:size}{report-body}{report-block}
```

I've changed report-info to print out the titles of all associated wiki pages, and that does work... not getting a total count though.

Thoughts?

Dave


Answers:
author: David Peterson
body: Hi Dave,

If you want the size, it's simpler using a {report-variable} instead, like so:

```
{report-variable:My Labeled Children}{local-reporter:value:label risk_open > label:content}   {content-filter:@self|scope="My Parent Page" > children} {local-reporter}{report-variable}{report-info:variable:My Labeled Children > collection:size} 
```

Hope that helps!

comments: author: David Dube
body: Awesome! Thanks David... works perfectly.


author: Sandro Herrmann [Communardo]
body: To count the child pages of a page with a specific label, please use a usermacro.

```
## @param label:title=label|type=string|required=true|desc=The label of the childpages#set($pageCount = 0)#foreach( $child in $content.children )       #foreach( $childlabel in  $child.labels )           #if($childlabel.name == $paramlabel )            #set($pageCount = $pageCount +1)        #end    #end#endThere are $pageCount Child pages with label $paramlabel
```

You can call this macro with "{usermakroname:label=labeltosearchfor}". I have testet this code with my Confluence 4.

comments: author: congming wang
body: Hey, i'm new confluence. If i want to count pages from all decendants within a pointed label. how can i update your code. In my mind , define a function with recursion to caculate all of pages from decendanta within pointed label, but tried without result. Here is my code :

## @param label:title=label\|type=string\|required=true\|desc=The label of the childpages  
#set($pageCount = 0)   

#marco(getTotal $node, $pageNumber)  
#foreach( $child in $node.children )   
#foreach( $childlabel in $child.labels )   
#if($childlabel.name == $paramlabel )  
#set($pageCount = $pageCount +1)  
#end  
#if($child.children.size\>0)  
#getTotal($child,$pageNumber)  
#else  
#end  
#end  

#getTotal($content, $pageNumber)

Please help!!!


author: Fabian Schubert
body: Hi,the macro to count the children of the CURRENT PAGE by label works fine for me.Now I would like to count the children of a SPECIFIC PAGE by label. This specific page is being spefied as a parameter: ## @param parentpage:title=Page\|type=confluence-content\|required=true\|desc=Select a page which children to countThen I use "$paramparentpage.children" to find the children of $parentpage:#foreach( $child in $paramparentpage.children ) But I am always getting 0 as a result for the number of child pages.Any hint very much appreciated.Here is the full macro:## @param parentpage:title=Page\|type=confluence-content\|required=true\|desc=Select a page which children to count## @param label:title=label\|type=string\|required=true\|desc=Specify a label to count#set($pageCount = 0)#foreach( $child in $paramparentpage.children ) #foreach( $childlabel in $child.labels ) #if($childlabel.name == $paramlabel ) #set($pageCount = $pageCount +1) #end #end#end$parampage$paramlabel: $pageCount


author: Stephen Deutsch
body: This should work:

```
## @param ParentPage:title=Page|type=confluence-content|required=true|desc=Select a page for which to count the children ## @param Label:title=label|type=string|required=true|desc=Specify a label to count #set($pageCount = 0) #set ( $colonIndex = $paramParentPage.indexOf(":") ) #if ( $colonIndex == -1 )     #set ( $parentSpaceKey = $space.key )     #set ( $parentPageName = $paramParentPage ) #else     #set ( $parentSpaceKey = $paramParentPage.substring(0, $colonIndex) )     #set ( $parentPageNameIndex = $colonIndex + 1 )     #set ( $parentPageName = $paramParentPage.substring($parentPageNameIndex) ) #end #set ( $parentPage = $pageManager.getPage($parentSpaceKey, $parentPageName) ) #foreach( $child in $parentPage.descendents )     #foreach( $childlabel in $child.labels )         #if($childlabel.name == $paramLabel )             #set($pageCount = $pageCount +1)         #end     #end #end <p>Under $parentPageName there are $pageCount pages with the label $paramLabel</p>
```


author: Fabian Schubert
body: Thanks a lot, Stephen! The macro now does its job perfectly! This was a great help!


author: Kevin Godsey
body: I'm looking for a variation of this that would return the number of active pages in a space based on the spacekey passed as a parameter? I've looked at a lot of examples but nothing seems to work. I keep getting total pages (including deleted, previous versions, etc.) instead of just the number of current active pages in the space.

Any help would be greatly appreciated!


author: Tansu Akdeniz
body: ![](../../upfiles/counter.png)

Hi

I want to write a macro that counts only descandants. For this example, the result is 4. We don't have to give the parent name parameter as a "Quotes" , we can just give labels to these 4 pages. How can I do it ?

Thanks in advance

Tansu