Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using labelManager in user macro

Priska Aprilia
Contributor
July 16, 2020 edited

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
Contributor
July 16, 2020 edited

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 edited

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
atlassian, atlassian community, job interviews, interview tips, career advice, interview preparation, job search, interview experiences, hiring process, atlassian careers, community discussion, interview best practices

Interview Tips & Pet Peeves ✍️

Join the discussion on interview best practices and common pitfalls. Share your experiences and learn from others in the Atlassian Community.

Share you experiences!
AUG Leaders

Atlassian Community Events