Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to fetch the Labels programmatically for a custom plugin in confluence

Vishal K S June 28, 2019

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.

2 answers

1 accepted

0 votes
Answer accepted
Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 29, 2019

Don't know about AJS, but there is a method and property, along the lines of:

$page.getLabels()
Vishal K S June 30, 2019

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?

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 30, 2019

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.

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

Vishal K S June 30, 2019

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?

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 1, 2019

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

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

Vishal K S July 1, 2019

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.

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 1, 2019

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.

Like # people like this
Vishal K S July 11, 2019

Thanks Bill. This helps.

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

Like Davin Studer likes this
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2019

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

Like Bill Bailey likes this
0 votes
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 28, 2019

You can use the REST API to get that info. The REST endpoint is ...

http(s)://{your server}/rest/api/content/{page id}/label
Vishal K S June 29, 2019

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?

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 1, 2019

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.

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 1, 2019

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.

Like Vishal K S likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events