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

How do I retrieve a list of spaces with a particular space-label?

Brett Ryan
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 30, 2012

I am trying to retrieve all spaces with a given space label

// How do I add labels filter to the query?
SpacesQuery sq = SpacesQuery
        .newQuery()
        .withSpaceType(SpaceType.GLOBAL)
        .sortBy("name")
        .build();
spaces = permissionManager.getPermittedEntities(
    user, Permission.VIEW, spaces);
for (Space s : spaceManager.getAllSpaces(sq)) {
    // Process space.
}

To be clear I don't want pages with labels, just the spaces themselves.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Remo Siegwart
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 30, 2012

We use the following code in some of our plugins:

private List<Space> getSpacesWithLabel(String label) {
    List<Space> spaces = new ArrayList<Space>();

    // build search query parameters
    SearchQueryParameters params = new SearchQueryParameters();
	Set<ContentTypeEnum> types = new HashSet<ContentTypeEnum>(Arrays.asList(ContentTypeEnum.SPACE_DESCRIPTION, ContentTypeEnum.PERSONAL_SPACE_DESCRIPTION));
    params.setContentTypes(types);
    Set<String> labels = new HashSet<String>(Arrays.asList(label));
	params.setLabels(labels);
    params.setSort(TitleSort.ASCENDING);

    // build search query
    ISearch search = predefinedSearchBuilder.buildSiteSearch(params, 0, Integer.MAX_VALUE);

    try {
        // execute search query
        SearchResults searchResults = searchManager.search(search);

        // store results
        for (SearchResult searchResult : searchResults) {
            Space space = spaceManager.getSpace(searchResult.getSpaceKey());
            spaces.add(space);
        }
    } catch (InvalidSearchException e) {
        // shouldn't happen
    }

    return spaces;
}

Hope this helps

Brett Ryan
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 30, 2012

Thanks Remo, how do I get an instance of `predefinedSearchBuilder`? I tried an injectable property and it came in as null.

Remo Siegwart
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 30, 2012

The com.atlassian.confluence.search.service.PredefinedSearchBuilder should be injectable. You need the following components for the above code to work:

private SearchManager searchManager;
private PredefinedSearchBuilder predefinedSearchBuilder;
private SpaceManager spaceManager;

...

public void setSearchManager(SearchManager searchManager) {
    this.searchManager = searchManager;
}

public void setPredefinedSearchBuilder(PredefinedSearchBuilder predefinedSearchBuilder) {
    this.predefinedSearchBuilder = predefinedSearchBuilder;
}

public void setSpaceManager(SpaceManager spaceManager) {
    this.spaceManager = spaceManager;
}
Brett Ryan
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 30, 2012

My bad, I actually tried to inject `DefaultPredefinedSearchBuilder`, thanks :)

Now I'm not getting any exceptions, however no spaces are comming up in the list. I've added a space label of "top" to the spaces to appear, and calling the method with "top".

I'm using Confluence 4.2.4.

Thanks for your help Remo, it is very much appreciated.

Brett Ryan
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 30, 2012

I also tried adding `ContentTypeEnum.SPACE` to the types set.

Brett Ryan
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 30, 2012

Actually, just took out the `params.setLabels(labels);` and all labels appear as I expected, so maybe I'm not supplying labels correctly? Maybe they need to be upper case?

Remo Siegwart
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 30, 2012

Make sure you always add the namespace to the label, for example:

  • "global:top" for the global label "top"
  • "team:department" for the team label (aka. space category) "department"
  • etc.
Remo Siegwart
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 30, 2012

No worries, I should have mentioned it in my answer.

Brett Ryan
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 30, 2012

Thank you so much Remo, this is just the ticket.

I must apologise for my lack of experience in this, I couldn't find anywhere in the API docs on how to do this and wouldn't have been able to without your help.

Again, thank you!

Brett Ryan
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 30, 2012

Hah, nah, it's cool. I was looking through the facilities from `List<Label> labels = labelManager.getLabels(Arrays.asList("top"));`, I then printed out all the properties and did see that 'global' was part of the namespace and made me realise there was more to the name.

This poking around though has led me to find `LabelManager.getSpacesWithLabel(String)`; which led me to the following:

List&lt;Label&gt; labels =
        labelManager.getLabels(Arrays.asList("top"));
List&lt;Space&gt; spaces = labels.isEmpty()
        ? Collections.&lt;Space&gt;emptyList()
        : labelManager.getSpacesWithLabel(labels.get(0));

Which, seemingly works just as well and is pretty simple; though, I'm sure you'll tell me why it's not, and I'm guessing it could be to do with the global vs team vs page labels etc, I could inadvertantly find a label on a page instead of a space I suppose?

I guess that could be fixed by traversing through the returned list and looking for the one with "global" :)

Remo Siegwart
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 30, 2012

Ah yeah, thanks for the pointer! This looks much simpler. I remember using this method at first, but there was probably the requirement to search for multiple labels.

The code I posted is the same used in the "Space Directory" of Confluence.

Remo Siegwart
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.
September 29, 2012

Now i know the difference of these two methods again. LabelManager.getSpacesWithLabel() returns all spaces with that label, even when the current user doesn't have the permissions to access some of the spaces. The method i've posted in my answer only returns the permitted spaces for the current user. That was the reason why we ended up using this method.

TAGS
AUG Leaders

Atlassian Community Events