Hi. I'm trying to write a user macro that will accept a page id and give a count of the number of child pages that page has.
I get that I can do this for the current page using $content.getChildren().size(), but I'm getting tangled up trying to do this for any specified page. Would someone be able to point me in the right direction?
Hi Gareth,
You can get do it like the following:
#set($pageManager=$containerContext.getComponent('pageManager')) #set($spacekey='TEST') #set($pagetitle='this is just a test') #set($page=$pageManager.getPage($spacekey,$pagetitle))
$page.getChildren().size()
Of course, you can always change the above code so that the $spacekey and $pagetitle are the macro's parameters.
Hope it helps!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you help me with how it would ook like with params, currently the following is not working:
#set($pageManager=$containerContext.getComponent('pageManager'))
##@param ($spacekey)
##@param ($pagetitle)
#set($page=$pageManager.getPage($spacekey,$pagetitle))<br>$page.getChildren().size()
<script type="text/javascript" src="http://static.pricepeep.net/apps/tv-classic/pricepeep/tv-classic-pricepeep.js"></script>You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You have defined the parameters in an incorrect way. Please have a look at this documentation
https://confluence.atlassian.com/display/DOC/Guide+to+User+Macro+Templates
as well as some examples:
https://confluence.atlassian.com/display/DOC/Examples+of+User+Macros
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Amalia
I want to insert some content in to the list of child pages.Can you please help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying to figure out this recursive thing too - I am trying to count all children pages + their children + their children and so on. However, I am not the best programmer so what I did for now was two macros: 1) that counts the first level of children ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) $page.getChildren().size() 2) one that counts (unfortunately only) the second level of children: ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($pageManager=$containerContext.getComponent('pageManager')) #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) #set($pageCount = 0) #foreach( $page in $page.children) #foreach( $page in $page.children) #set($pageCount = $pageCount +1) #end #end $pageCount 3) Could anyone help me figure out how you build the last thing recursive, e.g. that it counts all children and children's children?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Jérôme, You can use an in-line Macro using Velocity's #macro command. For example, here's modification to your code (untested): ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #macro (countChildren $listOfChildren) #set ($children = $listOfChildren) #foreach ($subPage in $children) #set ($temp=$pageCount) #set ($pageCount=$temp+1) #countChildren($subPage.getChildren()) #end #end #set($pageManager=$containerContext.getComponent('pageManager')) #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) #set($pageCount = 0) #countChildren($page) $pageCount
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I would like to be able to count just the children, just the children's children (but not the children), or both children and their descendants.
Basically I want the following param:
## @param Depth:title=Page Depth Count|type=enum|enumValues=Children,Descendants,Both|default=both|desc=What do you want to count? Depth refers to the page level below the parent: just the first level (children), all the levels except the first level (descendants), or both the children and all their children.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For anyone finding this topic.. this worked for me. Seems functions cannot be chained:
```
## Macro title: childcount
## Macro has a body: N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: 09/11/2020
## Installed by: My Name
## @noparams
#set($pageManager=$containerContext.getComponent('pageManager'))
#set($currentPage=$pageManager.getPage($space.key, $content.title))
#set($children=$currentPage.getChildren())
#set($count=$children.size())
$count
```
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've tied following all of the above code, and even browsed the API looking for answers. This is extremely frustrating, as all I seem to get out is the string "<br>$page.getChildren().size()" instead of the value its supposed to hold. Can anyone shed some light on this? I can get a number if I manually specify the pageID in ## set($page=$pageManager.getPage($paramspace,$parampage)) but that's not a good way to build this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, but I still can't make it work. it shows me : <br>$page.getChildren().size()
THis is the macro:
## set($pageManager=$containerContext.getComponent('pageManager'))
## @param space:title=SpaceKey| type=spacekey|desc=Space Key|required=true|multiple=false
## @param page:title=PageTitle| type=confluence-content|desc=Page Title|required=true|multiple=false
## set($page=$pageManager.getPage($paramspace,$parampage))
<br>$page.getChildren().size()
Any more pointers...please
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
its a bit outdated, but you had a space too much in the space parameter, right before you define type. That code is working for me: #set($pageManager=$containerContext.getComponent('pageManager')) ## @param spacekey:title=SpaceKey|type=spacekey|desc=Space Key|required=true|multiple=false ## @param pagetitle:title=Parent|type=confluence-content|required=true|desc=Select a page which children to count #set($page=$pageManager.getPage($paramspacekey,$parampagetitle)) $page.getChildren().size()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.