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

How to add labels to confluence page via user macro?

Before Confluence 7.1.0 we used the following, to automatically add labels in a user macro:

#set($labelManager=$containerContext.getComponent('labelManager'))
#set($success = ${content.labelUtil.addLabel('label-text', $action.labelManager, $content)})

 This does not longer work with the update to Confluence 7.1.0. There seems to be no label manager anymore. 

 

How to programatically set labels via user macro in Confluence 7.1.0?

1 answer

Hey @Michael Rosenberger,

the "com.atlassian.confluence.labels.LabelManager#addLabel(com.atlassian.confluence.core.ContentEntityObject;com.atlassian.confluence.labels.Label;) int" method is one of the deprecated methods that have been removed with Confluence 7.

From looking at the Java docs it seems like you'll need to use the createLabel() method instead. However, since this method expects to be passed an object of the type Label I'm not sure whether it is feasible to use this API in a user macro.

Cheers,
Sven

Hey,

thank you  for your helpful replay. Unfortunately I am still not able to use the labelManager in the user macro. I think the problem is, that it not gets instantiated correctly. 

I created a test macro with the following code. It does not remove the labels of the page where it is inserted. I first tired to add a label, but with no luck. No I used the removeAllLabels()-Method to test, but it also does not work.

#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($success = $labelManager.removeAllLabels($content))

 How to initialize and use the labelManager correctly?

Sorry, no idea. I think you should probably write an actual Java macro and let Confluence inject the LabelManager properly into your Macro class. This would be a lot easier and more flexible than trying to poke around with reflection inside a user macro.

Cheers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Posted in Confluence

Lessons and Learnings: Six Months of Working Remote [Discussion]

Hey there, folks! For most of us, the past six months- yes, you read that right- have been a journey. More people than ever before have pivoted to working remotely, and navigating being on-scre...

5,344 views 4 6
Join discussion

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