User Macro Add parameter value to AddLabel


I have a requirement to add the value of a parameter to a page as a label. 

The user macro is a subset of fields we are asking our users to provide.  In this example We are asking for a Document Number. The user provides this number and I wanted to add this as a label to the page.  The macro below includes a script that adds a Box with a plus icon. The user would enter the label and click on add.  I thought that I could reuse some of this macro to add a label but haven't been able to achieve this.   Ideally the user would not be able to see the input box or the add icon. The code would take the $paramDocNum and add it to the label.

Any suggestions would be greatly appreciated.  


## @param DocNum:title=Document Number |type=string|required=false|desc=Enter your Document ID #

var addLabel = function(label) { + "/json/addlabelactivity.action", {"entityIdString": "$", "labelString": label, "atl_token": jQuery("#atlassian-token").attr("content") }, function() {
<form class="aui" onsubmit="return false;">
<input id="label-to-add" class="text">
<button class="aui-button aui-button-subtle" onclick="addLabel(jQuery('#label-to-add').val());">
<span class="aui-icon aui-icon-small aui-iconfont-add">Add </span>

<td>Document ID #<br></td>
<td> $paramDocNum


1 answer

1 accepted

2 votes
Accepted answer

Hi Melissa,

This should work:

## @param DocNum:title=Document Number|type=string|required=false|desc=Enter your Document ID #

#if ( $paramDocNum )
    #set ( $hasLabel = false )
    #foreach ( $label in $content.getLabels() )
        #if ( $label == $paramDocNum )
            #set ( $hasLabel = true )
    #if ( !$hasLabel )
            AJS.toInit(function() {
       + "/json/addlabelactivity.action", {
                    "entityIdString": "$",
                    "labelString": "$paramDocNum",
                    "atl_token": jQuery("#atlassian-token").attr("content")
                <th>Document ID #</th>
                <td> $paramDocNum</td>

@Stephen Deutsch

Thank you!!!! I can't thank you enough! I would of never figured this out!

If I wanted to add the label to the child pages could I add this ?

#set($childpages = $content.getChildren())
#foreach($page in $childpages)

Hi Melissa,

It would work to insert those two lines after the first line, add an #end at the end of the document, and change $ to $, but this would only add labels to the children and not to the page itself. Therefore I would recommend to copy the body code (i.e. excluding the first line), make the changes that I mentioned (also remove the <table> tags and everything in between so that the DocNum doesn't display multiple times), and paste this underneath the existing code.

You should be aware of 2 more things:

1 - getChildren gets only the direct children of the page in question, any children of the children are not included:


  -Child (included)

     -Child of Child (not included)

If you want to also include the child of the child, this is referred to in the internals of Confluence as a "descendant", so instead of getChildren, you would call getDescendants.

2 - This checks every label of a page, so if you include checking all children/descendants and there are a lot of pages, it could slow down loading (probably not, but it might).

Thanks Again @Stephen Deutsch

I made your changes and it's working !

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Feb 06, 2019 in Confluence

Try out the new editing experience

Hi team, I’m Avinoam, a product manager on Confluence Cloud, and today I’m really excited to let the Community know that all customers can now try out the new editing experience and see some of the ...

793 views 33 5
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you