It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

Awesome! Thanks David... works perfectly.

1 vote

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 Jul 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 Jul 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 Sep 04, 2015 • edited Jun 21, 2017

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 Sep 06, 2015

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

Kevin Godsey 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 Sep 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

943 views 0 19
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you