Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can I get all labels used by a User?

Robert Krause
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.
May 28, 2015

Hello,

I am trying to get a list of all labels and the number of times they were used for each user. The LabelManager offers some methods that seem promising, but when testing them, they did not return the labels I just created.

 

I tried the following:

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import com.atlassian.confluence.labels.Label;
import com.atlassian.confluence.labels.LabelManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.google.common.collect.Lists;


@Path("/labeltest")
public class TestResource {

	LabelManager labelManager;

	public TestResource(final LabelManager labelManager) {
		this.labelManager = labelManager;
	}

	@GET
	public Response get() {
		final ConfluenceUser user = AuthenticatedUserThreadLocal.get();

		final List<Label> labels = labelManager.getUsersLabels(user.getName());

		final List<String> labelNames = Lists.newArrayList();
		for (final Label label : labels) {
			labelNames.add(label.getName());
		}

		return Response.ok(labelNames).build();
	}
}

 

But this only returns

[
  "favourite"
]

The labels I just added to some content are not shown.

Do you have any suggestion how I can get a List of all labels created by a user and the number of times they are user by her?

 

Thank you,
Robert

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 29, 2015

I'm not sure you can - the call you are using pulls back the user's personal labels, not the global labels they may have happened to create.  Personal labels are not visible to other users.

I've a strong suspicion that global labels don't record the creator or date, just that they're on pages.  I think you'd have to trawl the page history records for label changes to get what you're looking for (and I'm not sure even that would do it)

Robert Krause
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.
May 29, 2015

As far as I can tell it looks like that. I guess I need a new approach.

0 votes
Stephen Deutsch
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.
May 29, 2015

Hi Robert,

As far as I know, getUsersLabels only returns personal labels, not global labels.  You can use labellings to figure out which user attached which label:

https://docs.atlassian.com/confluence/latest/com/atlassian/confluence/labels/Labelling.html

I found a function to get all user labeled content, but not necessarily the labels themselves (findAllUserLabelledContent):

https://docs.atlassian.com/confluence/5.7/com/atlassian/confluence/labels/persistence/dao/LabelDao.html

My guess is that you could find all content labeled by a user, then use getLabelings, then step through all labellings until you find the user that matches the user you are looking for.

But maybe there is a better way that someone else is aware of...

Robert Krause
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.
May 29, 2015

I already tried using the LabelDao directly, but if I add the component-import, my plugin refuses to start.

TAGS
AUG Leaders

Atlassian Community Events