Forums

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

Retrieve all the pages within a space with ScriptRunner

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

 

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

2 answers

1 accepted

2 votes
Answer accepted
Evgenii
Community Champion
July 3, 2023

Hi, @Mouna Hammoudi 

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)
 
0 votes
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
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())

    }

   

}
Evgenii
Community Champion
July 3, 2023

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()}")
}
}
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

@Evgenii 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

Evgenii
Community Champion
July 3, 2023

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 

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

@Evgenii where to go to find the log manager and view this?

Evgenii
Community Champion
July 3, 2023

Sorry, use logger.warn (not info)

You'll see result in Log tab

image.png

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

@Evgenii 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

Evgenii
Community Champion
July 3, 2023

@Mouna Hammoudi 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

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

@Evgenii 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.

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
Evgenii
Community Champion
July 3, 2023

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.

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

@Evgenii 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? 

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

}
Evgenii
Community Champion
July 3, 2023

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events