User Macro Add parameter value to AddLabel
Asked by Melissa C:

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>
```


Answers:
author: Stephen Deutsch
body: 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
```

comments: author: Melissa C
body: [@Stephen Deutsch](/t5/user/viewprofilepage/user-id/781490)

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


author: Stephen Deutsch
body: 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).


author: Melissa C
body: Thanks Again [@Stephen Deutsch](/t5/user/viewprofilepage/user-id/781490)

I made your changes and it's working !


author: Ph?m M?nh T??ng
body: Hoi [@Stephen Deutsch](/t5/user/viewprofilepage/user-id/781490)

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


Tags: label

user-macro