Code using the confluence API to display statistics about the pages in 1 given space
Asked by Mouna Hammoudi:

I would like to write some code in the scriptrunner console to display some statistics about the pages in a given confluence space. I would like to display the current info

    "PageName;UserPageCreatorID;UserPageCreatorFullName;LastModificationDate\n"

How can I do this?


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

You can use such script. It will list pages in required space to another page in confluence. ID of page with report is configured at line 15

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

// Set space key, pages in which you're trying to list
Space space = spaceManager.getSpace("TEST")
// Set page ID, where you want to add list of pages in space
Page resultPage = pageManager.getPage(2752521)
String result = ""

for (Page page : pageManager.getPages(space, true)) {
      if (page.getCreator() == null) {
         result += "${page.title};creator is empty<br/>"
      } else {
         String username = page.getCreator().getName()
         String fullName = page.getCreator().getFullName()
         result += "${page.title};${username};${fullName};${page.getLastModificationDate()}<br/>"
      }
}

resultPage.setBodyAsString(result)
pageManager.saveContentEntity(resultPage, null, null)
```

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

comments: author: Evgenii
body: And if you'll use such script, you'll receive formatted report:

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

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

// Set space key, pages in which you're trying to list
Space space = spaceManager.getSpace("TEST")
// Set page ID, where you want to add list of pages in space
Page resultPage = pageManager.getPage(2752521)
String result = ""

String header = """<table style="">
  <colgroup>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
  </colgroup>
  <thead>
    <tr>
      <th style="text-align: left;">
        <p>Page Name</p>
      </th>
      <th style="text-align: left;">
        <p>Creator Username</p>
      </th>
      <th style="text-align: left;">
        <p>Creator Full Name</p>
      </th>
      <th style="text-align: left;">
        <p>Last Modified</p>
      </th>
      <th style="text-align: left;">
        <p>Version</p>
      </th>
    </tr>
  </thead>
  <tbody>"""

String footer = """</tbody>
</table>"""

for (Page page : pageManager.getPages(space, true)) {
   if (page.getCreator() == null) {
      result += """<tr><td style="text-align: left;">${page.title}</td>
      <td style="text-align: left;">creator is empty</td>
      <td style="text-align: left;"></td>
      <td style="text-align: left;"></td>
      <td style="text-align: left;"></td></tr>"""
   } else {
      String username = page.getCreator().getName()
      String fullName = page.getCreator().getFullName()
      result += """<tr><td style="text-align: left;">${page.title}</td>
      <td style="text-align: left;">${username}</td>
      <td style="text-align: left;">${fullName}</td>
      <td style="text-align: left;">${page.getLastModificationDate()}</td>
      <td style="text-align: left;">${page.version}</td></tr>"""
   }
}

resultPage.setBodyAsString(header + result + footer)
pageManager.saveContentEntity(resultPage, null, null)
```

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


Tags: cloud

confluence-cloud