Once upon a time, things were simple. If you wanted to retrieve a page using the Confluence Java API, you simply called getPage(). Fetching Spaces was similarly easy, and intuitive.
Those days are over. The methods are deprecated. Instead, we now need to use SpaceService and ContentService to manage spaces and content, respectively. Let’s take a look at some examples of how a task would have been accomplished with the PageManager and SpaceManager, and compare that to how those tasks would be accomplished today.
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def spaces = spaceManager.getAllSpaces()
spaces.each { space ->
def pagesInSpace = pageManager.getPages(space, true)
pagesInSpace.each { page ->
log.warn(page.getBodyAsString())
}
}
Here’s the same code, using the SpaceService and ContentService classes:
import com.atlassian.confluence.api.model.Expansions
import com.atlassian.confluence.api.model.content.ContentRepresentation
import com.atlassian.confluence.api.model.content.ContentBody
import com.atlassian.confluence.api.model.content.Content
import com.atlassian.confluence.api.model.content.Space
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.pagination.PageResponse
import com.atlassian.confluence.api.service.content.SpaceService
import com.atlassian.confluence.api.service.content.ContentService
import com.atlassian.confluence.api.model.content.ContentType
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
def contentService = ScriptRunnerImpl.getPluginComponent(ContentService)
def spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
SimplePageRequest pageRequest = new SimplePageRequest(0, 10)
PageResponse < Space > spaceResults = spaceService.find(new Expansion('name')).fetchMany(new SimplePageRequest(0, 10))
List < Space >