User Macro Add parameter value to AddLabel

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>

 

1 answer

1 accepted

2 votes
Stephen Deutsch Community Champion Nov 03, 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

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

Thanks Again @Stephen Deutsch

I made your changes and it's working !

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Confluence

Three common content challenges + how to manage them

An efficient enterprise content management system, or ECM, is a must-have for companies that create work online (cough   cough, all companies). If content calendars, marketing plans, and bu...

42 views 0 4
Read article

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