Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Code using the confluence API to display statistics about the pages in 1 given space

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 3, 2023

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?

1 answer

0 votes
Evgenii
Community Champion
July 3, 2023

Hi, @Mouna Hammoudi 

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

Evgenii
Community Champion
July 3, 2023

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events