Retrieving 100 times more pages than what is actually available in the space
Asked by Mouna Hammoudi:

I am writing some code to print some information about a given confluence space and I would like to print some information about the pages. The problem is that the space only contains 403 pages.

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

When I use the following code, I get around 100 000 rows which is wrong since I should only be getting 403 rows. Here is my code. Anyone knows the problem?

```
?
```

```
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)

def file = new File('D:/confluence/data/scripts/result.groovy')

def fileWriter = new FileWriter(file) 

Space space = spaceManager.getSpace("IWIKI")

String result=""

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

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

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

    }

    else{

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

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

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

    }

   fileWriter.write(result)

}

fileWriter.close()
```


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

The issue in your code lies in the placement of the `fileWriter.write(result)` statement.

You are currently writing the result to the file inside the loop for each page, which accumulates the result string with each iteration.

To fix this, move the `fileWriter.write(result)` statement outside the loop so that it writes the final result after all the pages have been processed.

Here's the modified 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)

def file = new File('D:/confluence/data/scripts/result.groovy')
def fileWriter = new FileWriter(file) 
Space space = spaceManager.getSpace("IWIKI")

String result = ""

for (Page page : pageManager.getPages(space, false)) {
if (page.getCreator() == null) {
result = result + page.toString() + ",null" + "\n"
} else {
String userID = page.getCreator().getName()
String fullName = userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
result = result + page.toString() + "," + userID + "," + fullName + "," + page.getLastModificationDate() + "\n"
}
}

fileWriter.write(result)
fileWriter.close()
```

Now, the `fileWriter.write(result)` statement will only be executed once, after processing all the pages in the loop. This will ensure that the result is written to the file correctly, and you should get the expected output with 403 rows instead of 100,000.

/Markus


Tags: confluence-server

server