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.
Don't know about AJS, but there is a method and property, along the lines of:
$page.getLabels()
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Bill. This helps.
I wasn't aware that we could insert into the site layouts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can use the REST API to get that info. The REST endpoint is ...
http(s)://{your server}/rest/api/content/{page id}/label
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.