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.
Community moderators have prevented the ability to post new answers.
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
Thanks Remo, how do I get an instance of `predefinedSearchBuilder`? I tried an injectable property and it came in as null.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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; }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I also tried adding `ContentTypeEnum.SPACE` to the types set.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sure you always add the namespace to the label, for example:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No worries, I should have mentioned it in my answer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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<Label> labels = labelManager.getLabels(Arrays.asList("top")); List<Space> spaces = labels.isEmpty() ? Collections.<Space>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" :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.