This article is for users of the Confluence Java API in ScriptRunner or other places the Java API may be used. The article does not apply to the REST API.
Sorry, but I am still at a loss as to how to get a space object in Velocity in a User Macro. If you know, please share in a comment.
Atlassian has deprecated the spaceManager.getSpace(), spaceManager.getSpaces(), and other similar space object instatiators (yes, that's now a word because I said so).
Since it took piecing together multiple Community answers and a lot of trial and error, I thought I'd write this article to share so that others don't have to go through as much trouble as I did just to get a space object now.
Note: if there's an easier way to do this, I'm all for it. Please let me know in a comment.
To make it cleaner, I'll show you the two ways to get the SpaceService, then use one of them in the examples. They're about the same.
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
import com.onresolve.scriptrunner.runner.customisers.PluginModule
@PluginModule
SpaceService spaceService
import com.atlassian.confluence.api.model.content.Space
// this is different from com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.service.content.SpaceService
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
// get SpaceService to use to find spaces
SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
// put the space key you're searching for here, or directly where the variable is used
String spaceKey = "ABC"
// here's the tricky part, just go with it
Space space = spaceService.find(new Expansion('name'))
.withKeys(spaceKey)
.fetch()
.get()
// just in case no spaces are found with the keys provided
if (!space) {return "No space found."}
// get the space info needed
return "<p>${space.getKey()}: ${space.getName()}</p>"
import com.atlassian.confluence.api.model.content.Space
// this is different from com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.model.pagination.PageResponse
import com.atlassian.confluence.api.service.content.SpaceService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
// declare result variable
String result = ""
// here's the tricky part, just go with it
PageResponse<Space> spaceResponse = spaceService.find(new Expansion('name'))
.withKeys("ABC","TEST") // put the space keys you're searching for here
.fetchMany(new SimplePageRequest(0, 10)) // paginate and tell it how many results you want back
// just in case no spaces are found with the keys provided
if (!spaceResponse) {return "No spaces found."}
// now turn the response into a list of spaces (why oh why so many steps?)
List<Space> spaces = spaceResponse.getResults()
// loop through the list of spaces
spaces.each { space ->
// get the space info needed
result += "<p>${space.getKey()}: ${space.getName()}</p>"
}
return result
There you have it. From spaceManager.getSpace() to all that mess above. There must be some underlying reason for it, but I'm not privy to it.
I'll probably create a method to call that's like .getSpace() to make it easier on me. If I do, I'll try to remember to post it here or in a new article.
Update: See Get spaces by keys with the Confluence API Part 2
If this article makes your life a little easier, I'd appreciate a Like from you.
This article was written by little ol' me. So please don't complain to Atlassian or Adaptavist for any mistakes I may have made. Feel free to complain about the complexity of the deprecation replacement to Atlassian, though.
I'll try to answer any questions that come up, but I may win the lottery one day, if I ever decide to play it.
WW
9 comments