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:
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
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)
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())
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()}")
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Evgenii thanks! How can I access the whole log? As you can see below, only the first 300 lines are shown in the console.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Evgenii where to go to find the log manager and view this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.