How do I add labels to a page using a user macro?

Sean King
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2011

I'm trying to build a user macro that allows the user to add specific labels to a page using checkboxes. Is this posible because I can't seem to get it to work.

Even skipping the checkbox part, how do i get a user macro to add a label to a page?

2 answers

1 accepted

3 votes
Answer accepted
Sandro Herrmann [Communardo]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 23, 2011

I would use the label manager to create the checkbox list of label. Afterward on checkbox click i would call the original add label request via ajax. The following user macro code is a quick implement and is working for my Confluence 4. Maybe you can take this as a starter.

## @noparams
 
<script type="text/javascript">

AJS.toInit(function ($) { 

//every label checkbox on the page
jQuery(".custom_label_to_add").click(function(){

//call the add label ajax request
    jQuery.post(contextPath+"/json/addlabelactivity.action", {"entityIdString": "$content.id", "labelString": jQuery(this).val(), "atl_token": jQuery("#atlassian-token").attr("content") } ,function(){

        alert("label added to page");
    });

});



});



</script>

 
#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')) 

Popular Labels

#foreach( $popularLabelResult in $labelManager.getMostPopularLabels() )
<br>       
<input type="checkbox" class="custom_label_to_add" value="$popularLabelResult.label.name"> $popularLabelResult.label.name
 

#end

Sean King
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2011

I was actually wanting to have the checkboxes(determine the $param's for the labels) inside the macro browser interface and not on the actual page. I'm wanting my users to use the macro browser, pick their labels, and then when they click insert it'll apply the labels to the page(or when they save the page). I already have the user macro reading the page labels and populating a div based on certain values found, but I can't seem to figure out how to add labels from the macro browser with a user macro.

Sven
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2011

really helpful. Thanks!

That helped my as starting point for this:

https://answers.atlassian.com/questions/25933/confluence-user-macro-popular-labels-with-new-label-filter-why-is-this-not-working-on-the-current-space

(if you have any idea whats going wrong there...)

Like David Rawsthorne likes this
0 votes
Anselmo March 20, 2014

Hi folks,

this is a very honorable issue, which could help a lot for having reliable display in all pages working with the contentbylabel macro. I highly appreciate your efforts, but I'm not into coding by myself.

If you are about to offer a prototype or evaluated application, please consider the idea to distribute labels also downwards into all child pages. I can't see, why this is not available for a long time already.

From my point of view this is a very natural way how knowledge grows: First you distinguish step by step and then you want to mark all the stuff as belonging together somehow. In former times this was represented by folders with classical restrictions. Labels offer the way of having multidimensional structures. But it is too much work, to keep track of having them in really every single page. Some tools are definitely needed to facilitate the handling of documentation metadata.

Suggest an answer

Log in or Sign up to answer