Retrieve all the pages within a space with ScriptRunner
Asked by Mouna Hammoudi:

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:

![Capture.PNG](/t5/image/serverpage/image-id/266296iF84696EF406A6EEF/image-size/large?v=v2&px=999 "Capture.PNG")

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**


Answers:
author: Evgenii
body: Hi, [@Mouna Hammoudi](/t5/user/viewprofilepage/user-id/4688138)

Please, try this code:

```
/*
 * Created 2023.
 * @author Evgeniy Isaenkov
 * @github https://github.com/Udjin79/SRUtils
 */

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.Space

SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)
PageManager pageManager = ComponentLocator.getComponent(PageManager)

String spaceKey = "IWIKI"
Space wikiSpace = spaceManager.getSpace(spaceKey)
List<Page> pages = pageManager.getPages(wikiSpace)
?
```


author: Mouna Hammoudi
body: ```
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())

    }

   

}
```

comments: author: Evgenii
body: Slightly modified. Small advice, use variables in strings, not concatenation.

```
/*
 * Created 2023.
 * @author Evgeniy Isaenkov
 * @github https://github.com/Udjin79/SRUtils
 */

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.user.UserAccessor
import org.apache.logging.log4j.Logger
import org.apache.logging.log4j.LogManager

Logger logger = LogManager.getLogger("my.logger")

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) {
      logger.warn("${page.toString()} is null")
   } else {
      String userID = page.getCreator().getName()
      String fullName = userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
      logger.warn("${page.toString()}, ${userID}, ${fullName}, ${page.getLastModificationDate()}")
   }
}
```


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514) thanks! How can I access the whole log? As you can see below, only the first 300 lines are shown in the console.

![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/266327i4945A78B7F4781B8/image-size/large?v=v2&px=999 "Capture.PNG")


author: Evgenii
body: To solve it - you must add all strings to one, and then push it to Result. You can make it so:

```
/*
 * Created 2023.
 * @author Evgeniy Isaenkov
 * @github https://github.com/Udjin79/SRUtils
 */

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.user.UserAccessor

SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)
PageManager pageManager = ComponentLocator.getComponent(PageManager)
UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)
Space space = spaceManager.getSpace("IWIKI")
String outLog = ""

for (Page page : pageManager.getPages(space, true)) {
   if (page.getCreator() == null) {
      outLog += "${page.toString()} is null<br/>"
   } else {
      String userID = page.getCreator().getName()
      String fullName = userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
      outLog =+ "${page.toString()}, ${userID}, ${fullName}, ${page.getLastModificationDate()}<br/>"
   }
}

outLog
```

\\n - it's for making newline


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514) where to go to find the log manager and view this?


author: Evgenii
body: Sorry, use logger.**warn**(not info)

You'll see result in Log tab

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/266328iAD4277ADA13752F2/image-size/large?v=v2&px=999 "image.png")


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514) you are not answering my question, there are more than 300 entries where should I go and click to acess the whole list of entries being printed. I went to Logging and Profiling but couldn't find anything

![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/266333i31445CF52FB27C91/image-size/large?v=v2&px=999 "Capture.PNG")


author: Evgenii
body: [@Mouna Hammoudi](/t5/user/viewprofilepage/user-id/4688138) you're running script in console, right?

In console, you have 3 tabs, Result, Logs and Timing. You don't need to go to **Logging and Profiling.**Look at Result tab in Console.

Run my code above (3 messages upper) in Console and look at Result tab. I slightly corrected it, removed some unneeded code

UPD. Was mistaken, about log limits, fixed solution


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514) I know but I have more than 300 entries and I am not able to view everything. The content of the log tab is truncated after 300 lines.


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514)

![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/266345i44F98DCECF54D7C8/image-size/large?v=v2&px=999 "Capture.PNG")


author: Evgenii
body: Please, try my code. Look at stings:

**String outLog = ""**

for (Page page : pageManager.getPages(space, true)) {  
if (page.getCreator() == null) {  
**outLog += "${page.toString()} is null\<br/\>"**   
} else {  
String userID = page.getCreator().getName()  
String fullName = userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()  
**outLog =+ "${page.toString()}, ${userID}, ${fullName}, ${page.getLastModificationDate()}\<br/\>"**   
}  
}

**outLog**

With this code, you will have one string, not 300+. Scriptrunner have limit, how many log entries are shown. But if there is one formatted string, it will show it, no matter how it is formatted and how long it is.


author: Mouna Hammoudi
body: [@Evgenii](/t5/user/viewprofilepage/user-id/3993514) OK but I would like to avoid this and have everything shown nice and clean in a real log. Could you tell me know to access it?


author: Mouna Hammoudi
body: ```
@Evgenii?still truncated with this 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 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")

String result=""

for (Page page : pageManager.getPages(space, true)) {

    if(page.getCreator()==null){

            result=result+page.toString()+",null"+""

    }

    else{

            String userID=page.getCreator().getName()

            String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()

            result=result+page.toString()+","+userID+","+fullName+","+page.getLastModificationDate()+""

    }

   log.warn(result)

}
```


author: Evgenii
body: Try this:

```
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.user.UserAccessor

SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)
PageManager pageManager = ComponentLocator.getComponent(PageManager)
UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)

Space space = spaceManager.getSpace("IWIKI")
String result = ""

for (Page page : pageManager.getPages(space, true)) {
   if (page.getCreator() == null) {
      result += page.toString() + ",null" + "<br/>"
   } else {
      String userID = page.getCreator().getName()
      String fullName = userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
      result += page.toString() + "," + userID + "," + fullName + "," + page.getLastModificationDate() + "<br/>"
   }
}
result
```

Don't forget \<br/\>, this HTML tags make new lines


Tags: confluence-server

scriptrunner

server