Count child pages of parent when creating a page from template Edited

I'm trying to automatically set parts of the title in pages created with a macro, inspiation found here: https://community.atlassian.com/t5/Confluence-questions/How-to-count-the-number-of-child-pages-with-a-particular-label/qaq-p/445878

The macro I've written counts the child pages of the current parent, and returns that number + 1, indicating the numeric title of the page to be created

 

1 Parent (this page has a "create from template" button)
    1.1 Child one
    1.2 Child two
    1.3 Child three, etc.

The parent page has a "Create from template" button. When trying to use the `countChildren` macro when generating the `Child one` pages and so on from a Template, the returned number of children is zero. I suspect the macro doesn't recognize `1 Parent` as a parent page anymore. 

 

How do I refer to the `parent` page which calls the template in a macro?

## @noparams

#set($pageCount=0)
#foreach ($child in $content.children)
    #set($pageCount = $pageCount + 1)
#end
$pageCount  

 

1 answer

1 accepted

1 vote
Stephen Deutsch Community Champion Jun 21, 2017

Pretty easy change:

## @noparams

#set ( $pageCount = 0 )
#foreach ( $child in $content.parent.children )
    #set ( $pageCount = $pageCount + 1 )
#end
<p>$pageCount</p>

Thanks for the reply. This doesn't work in a template... Does the template page know its parent before it is "properly" created, i.e. it is available as a new page in the page tree? 

Stephen Deutsch Community Champion Jun 21, 2017

Unfortunately it doesn't, it only knows it as soon as the page is saved. At the moment of creation before it is saved, it acts as an "orphaned" page. I don't think there's anything that can be done about this :(

Thanks for the effort, mate :-) 

The more I try to translate what I consider pretty simple requirements when it comes to reducing administration by using Confluence, the less impressed I am. 

One thing that's nice about user macros is that just about nothing is impossible :)

This might work for you:

## @noparams

#set ( $Long = $generalUtil.systemStartupTime )

#set ( $templateParent = "" )
#set ( $referer = $req.getHeader("referer") )
#if ( $referer.contains("fromPageId=") )
    #foreach ( $part in $referer.split("&") )
        #if ($part.contains("fromPageId=") )
            #set ( $templateParent = $part.replace("fromPageId=", "") )
        #end
    #end
#end
#set ( $templateParent = $req.getParameter("fromPageId") )

#if ( $templateParent != "" )
    #set ( $parentPage = $pageManager.getPage($Long.parseLong($templateParent)) )
#else
    #set ( $parentPage = $content.parent )
#end

#set ( $pageCount = 0 )
#foreach ( $child in $parentPage.children )
    #set ( $pageCount = $pageCount + 1 )
#end
<span>$pageCount</span>

 

It works! Great! So thankful! :-D 

Where on earth did you find documentation for the magic you perform here?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Friday in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

287 views 11 10
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