Search code examples
confluenceconfluent-schema-registryconfluence-rest-apiscriptrunner-for-jiraconfluence-macros

Retrieve all the pages within a space with ScriptRunner


I am writing code in the script console to list the pages in a specific space called IWIKI. I am using the following code: 

import com.atlassian.confluence.pages.Page

import com.atlassian.confluence.pages.PageManager

import com.atlassian.confluence.spaces.Space

import com.atlassian.confluence.spaces.SpaceManager

import com.atlassian.sal.api.component.ComponentLocator

import com.atlassian.confluence.api.service.content.SpaceService

import com.atlassian.confluence.api.model.content.Space

import com.atlassian.confluence.spaces.Space

import com.atlassian.confluence.api.service.content.SpaceService

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def spaceManager = ComponentLocator.getComponent(SpaceManager)

def pageManager = ComponentLocator.getComponent(PageManager)

 

String spaceKey = "IWIKI"

SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)

// here's the tricky part, just go with it

Space space = spaceService.find().withKeys(spaceKey).fetch()

List<Page> pages = pageManager.getPages(space, true)

I am getting the following error:

enter image description here

Anyone knows what the problem is? I think spaceService.find().withKeys(spaceKey).fetch() is returning an object of type com.atlassian.confluence.api.model.content.Space and the next line pageManager.getPage(space, true) is expecting a space parameter of type com.atlassian.confluence.spaces.Space


Solution

  • import com.atlassian.confluence.links.OutgoingLink
    import com.atlassian.confluence.pages.Page
    import com.atlassian.confluence.pages.PageManager
    import com.atlassian.confluence.spaces.Space
    import com.atlassian.confluence.spaces.SpaceManager
    import com.atlassian.sal.api.component.ComponentLocator
    import org.apache.log4j.Logger
    import com.atlassian.confluence.user.UserAccessor
    import com.atlassian.sal.api.user.UserKey
    
    SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)
    PageManager pageManager = ComponentLocator.getComponent(PageManager)
    UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)
    
    Space space = spaceManager.getSpace("IWIKI")
    
    
    for (Page page : pageManager.getPages(space, true)) {
        if(page.getCreator()==null){
                log.warn(page.toString()+",null")
        }
        else{
                String userID=page.getCreator().getName()
                String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
                log.warn(page.toString()+","+userID+","+fullName+","+page.getLastModificationDate())
    
        }
    
       
    }