Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using labelManager in user macro

Priska Aprilia July 16, 2020

I am writing a user macro to get a list of pages with a specific label.

This is the code that I have written. But it does not return anything. Do you know what's wrong with the code? how can I use the labelManager in a user macro?

## Macro title: My Macro
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## This is an example macro
## @Param Label:title=Label|type=string|required=true|desc=Input one label

#set($containerManagerClass=$action.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)

#set($labelManager=$containerContext.getComponent('labelManager'))

##set($pages = $labelManager.getContentForLabel($labelManager.getLabel($paramLabel), 10))
<span>
#foreach( $page in $pages )
<span class="aui-label">$page.name</span>
#end

1 answer

1 accepted

0 votes
Answer accepted
Priska Aprilia July 16, 2020

I made a mistake..I had an extra character # in the $pages parameter definition.

 

I am writing a user macro to get a list of pages with a specific label.

This is the code that I have written. But it does not return anything. Do you know what's wrong with the code? how can I use the labelManager in a user macro?

## Macro title: My Macro
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## This is an example macro
## @Param Label:title=Label|type=string|required=true|desc=Input one label

#set($containerManagerClass=$action.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)

#set($labelManager=$containerContext.getComponent('labelManager'))

#set($pages = $labelManager.getContentForLabel($labelManager.getLabel($paramLabel), 10))
<span>
#foreach( $page in $pages )
<span class="aui-label">$page.title</span>
#end

Matthias Schouten March 15, 2021

No Idea if this is still needed, but you could use

#set( $labelManager = $action.getLabelManager())

to get the labelmanager for a user macro

 

$action is the ConfluenceActionSupport, which is provided within the velocity templates by confluence (https://developer.atlassian.com/server/confluence/confluence-objects-accessible-from-velocity/), and holds a labelManager object (https://docs.atlassian.com/ConfluenceServer/javadoc/7.11.1/com/atlassian/confluence/core/ConfluenceActionSupport.html)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events