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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Using labelManager in user macro

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

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

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
Community showcase
Published in Confluence

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,542 views 7 29
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you