User Macro Add parameter value to AddLabel

Melissa C November 3, 2017

Hello!

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.  

Melissa

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


<script>
var addLabel = function(label) {
jQuery.post(contextPath + "/json/addlabelactivity.action", {"entityIdString": "$content.id", "labelString": label, "atl_token": jQuery("#atlassian-token").attr("content") }, function() {
jQuery("#label-to-add").val("");
});
}
</script>
<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>
</button>
</form>

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

 

2 answers

1 accepted

2 votes
Answer accepted
Stephen Deutsch
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 3, 2017

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 )
        #end
    #end
    #if ( !$hasLabel )
        <script>
            AJS.toInit(function() {
                jQuery.post(contextPath + "/json/addlabelactivity.action", {
                    "entityIdString": "$content.id",
                    "labelString": "$paramDocNum",
                    "atl_token": jQuery("#atlassian-token").attr("content")
                });
            });
        </script>
    #end
    <table>
        <thead>
            <tr>
                <th>Document ID #</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td> $paramDocNum</td>
            </tr>
        </tbody>
    </table>
#end
Melissa C November 6, 2017

@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)
Stephen Deutsch
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 6, 2017

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 $content.id to $page.id, 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:

-Page 

  -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).

Melissa C November 6, 2017

Thanks Again @Stephen Deutsch

I made your changes and it's working !

0 votes
Phạm Mạnh Tường June 29, 2021

Hoi @Stephen Deutsch 

I'm trying to build a user macro that allows the user to add specific labels to a page using checkboxes. I will use this macro for the template

When user choose a macro, they can enter the label name and the label value

When user create a new document from template, they can see a checkbox

After checked on the checkbox and save, the label will be added to page

I tried from your macro but I can't make it work, so please give me a suggestion.

Thank you

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events