How to fetch the Labels programmatically for a custom plugin in confluence
Asked by Vishal K S:

How do I fetch all the labels for a page in confluence, through AJS? I can get it by going through the id of the labels, but is there an interface which gives all the labels of a particular page that I'm on?

The desired functionality is to get the labels, and perform some functionality based on the labels.

We have our confluence on a confluence server setup.


Answers:
author: Bill Bailey
body: Don't know about AJS, but there is a method and property, along the lines of:

```
$page.getLabels()
```

comments: author: Vishal K S
body: Hey Bill, what exactly is this $page and how do I reference this? Do I get this in my javascript or is it a java object?


author: Bill Bailey
body: Well my area of quasi expertise is user macros, so velocity and java objects. So $page was just a reference to a content entity object. And the method is a part of the Confluence [LabelManager.](https://docs.atlassian.com/atlassian-confluence/6.6.0/com/atlassian/confluence/labels/LabelManager.html)

So hoping I could point you in the right direction. I did find this exchange that might help point you in the right direction:

[How-can-I-remove-a-label-from-a-page-using-the-javascript](https://community.atlassian.com/t5/Answers-Developer-Questions/How-can-I-remove-a-label-from-a-page-using-the-javascript/qaq-p/541088)


author: Vishal K S
body: Ah, thanks a ton Bill. In this answer that you linked, the approach was to get it from the labels.

```
var labels = $(".aui-label-split-main");
```

The problem with this is, it would work in my current version, but in case we upgrade our confluence, they might end up changing their styles. But this is the closest to what I can get now, I believe.

So my aim is to read the labels and insert a particular message on the UI whenever any page is viewed. Correct me if I'm wrong, but the problem with using LabelManager for this is that to achieve this, I would need a "Pre Page View Event" to be triggered right?


author: Bill Bailey
body: Well some observations, the class names for the UI seem pretty stable, but it is a risk.

Also, not sure of your exact use case, but you may be able to do this with a user macro, where you could access the label manager.Basically, this is what the content by label macro does -- read the labels from the viewed page, then displays matching content.

You can also create a popup with a user macro (using AUI elements) if you want to display a message that can then be closed by the user ([dialog2.html](https://docs.atlassian.com/aui/8.3.3/docs/dialog2.html)).

And the development of a user macro is easier than for a plugin.


author: Vishal K S
body: Thanks Bill, this is helpful.

Just to give you context, my requirement is to get the labels which is present in the page, whenever the page is viewed. And if there is a particular label present, I would create a dialog box of sorts(AJS.messages), and display it to the user.

This functionality has to be system wide to all the pages. And the users needn't have to include the macro themselves.

Does user macro let me insert to all pages in confluence by default? I was under the impression that the macro is something we have to include manually while creating the document.


author: Bill Bailey
body: Well this used to be hard to do, but you can update the page layout (under Space tools -\> Layout or Admin Console -\> Edit Site Layouts), which is designed using Velocity.

I would suggest first developing your code as a macro, then moving parts of it to the page layout on a dev server, before modifying the mane site. And note that the page layout shows you how to detect wither in view or edit mode.


author: Vishal K S
body: Thanks Bill. This helps.

I wasn't aware that we could insert into the site layouts.


author: Davin Studer
body: You can add it into the layout (page decorator) using velocity like this ...

```
$action.getHelper().renderConfluenceMacro("{myMacroNameUsingWikiMarkup}")
```

This link shows how to use wiki markup for macros.

<https://confluence.atlassian.com/doc/confluence-wiki-markup-251003035.html>


author: Davin Studer
body: You can use the REST API to get that info. The REST endpoint is ...

```
http(s)://{your server}/rest/api/content/{page id}/label
```

comments: author: Vishal K S
body: Thanks Davin. But the problem is I don't want to be making API calls every time the javascript is called, it would be lots of API calls. Is there any other way to this?


author: Davin Studer
body: I would say if you don't want to use the API that they provide for this the only way that I know would be to parse the page DOM.


author: Davin Studer
body: If this is only an internal plugin another option would be to do something similar to what Bill has suggested. You could get the labels via velocity or java in a macro and include the macro on every page via the page decorator. The macro would simply output the labels as a JavaScript array. You would then have something reliable as to format.


Tags: app

aui

confluence-app

confluence-server

developer

server