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

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

Accepted Answer
0 votes

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!

Awesome! Thanks David... works perfectly.

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.

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

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

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>

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

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!

 

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
Community showcase
Published Oct 09, 2018 in Confluence

Introducing Praecipio Consulting, an Atlassian Solution Partner

Hey there Community!  My name is Vannya Vallejo, the Channel Communication Specialist at Atlassian and I want to help Atlassian users like you learn about our Solution Partners and how they c...

364 views 0 9
Read article

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