How to add labels to the current page in a self-written user makro?

I searched a lot, but nothing worked. There are downloadable Makros who manage to add labels you type into them, but as they´re not open source I can´t get behind that magic, which of course works, as I tested. Using the REST Api didn´t work, using the labelManager and labelUtil also lead to nothing. But I am sure there´s a way to use the labelManager to do so, maybe with the function "addLabel(Labelable content, Label label)" but there I don´t know with what to fill the parameters, beacause I have no access to the current page and I don´t know how to create a new label.

Thanks for your help

2 answers

I've tried adding labels via user macro, and the easiest thing I've found is to use ajax and let Confluence take care of the rest of the details.  Here is a simple user macro that adds a label based on what you type into a text field.

## @noparams
<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>

thank you very much it works fine for me :)

This is great!  Thanks for posting.  

Can I use a field in my macro example "Document Number" that populates the label for the page.  I'm pretty new to this so I'm no sure how to do that yet.  

I currently have my users populating a number of fields in a macro. I wanted to use the "Document Number" field to update the label of the page. 

what should I add to do this?

Thank you for this macro. It still works with Confluence 6.11.2.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

1,208 views 2 13
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