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

1 answer

This widget could not be displayed.
Stephen Deutsch Community Champion Mar 27, 2015

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?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

431 views 4 6
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