How to count the number of child pages with a particular label?

David Dube
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 27, 2011

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 > label:content} 

  {content-filter:@self|scope="My Parent Page" > children} 

{local-reporter}

{report-body}{report-info:@self > 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

3 answers

1 accepted

0 votes
Answer accepted
David Peterson
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 28, 2011

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!

David Dube
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 28, 2011

Awesome! Thanks David... works perfectly.

4 votes
Sandro Herrmann [Communardo]
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 28, 2011

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

#end


There 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.

congming wang July 16, 2012

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!!!

Fabian Schubert July 21, 2015

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 count Then 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

Stephen Deutsch
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.
September 4, 2015

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>
Like RobertM likes this
Fabian Schubert September 6, 2015

Thanks a lot, Stephen! The macro now does its job perfectly! This was a great help!

Kevin Godsey
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!
May 16, 2016

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!

 

1 vote
Tansu Akdeniz
Contributor
September 10, 2012

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events