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?
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)
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.