Example scripts
To homepage
Confluence

Move a Page when Created
App in script

ScriptRunner For Confluence
by Adaptavist
Compatibility

Confluence (7.15 - 8.6)

ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.confluence.event.events.content.page.PageCreateEvent
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.sal.api.component.ComponentLocator
def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def commentManager = ComponentLocator.getComponent(CommentManager)
def createEvent = event as PageCreateEvent
def originPageTitle = createEvent.page.title
def originSpaceKey = createEvent.page.spaceKey
def destinationPageTitle = '<YOUR_DESTINATION_PAGE_TITLE>' // name of the page in the new Parent space
def destinationSpaceKey = '<YOUR_DESTINATION_SPACE_KEY>' //new Parent Space
// only run the script on Origin Space
if ( originSpaceKey != '<YOUR_ORIGIN_SPACE_KEY>') {
return
}
def originalPage = pageManager.getPage(originSpaceKey, originPageTitle)
def destinationPage = pageManager.getPage(destinationSpaceKey, destinationPageTitle)
def destinationSpace = spaceManager.getSpace(destinationSpaceKey)
def pageExist = pageManager.getPages(destinationSpace, true).title.find { it == originPageTitle }
// if page already exist in the Destination Space, append the Title with -1
if ( pageExist ) {
log.info "The page ${originPageTitle} already exists in the '${destinationSpaceKey} Space'. Renaming the page to '${originPageTitle} - 1 '"
originalPage.setTitle(originPageTitle + ' - 1')
}
pageManager.movePageAfter(originalPage, destinationPage)
// add a Comment inside Origin Page to inform the User.
commentManager.addCommentToObject(originalPage, null, "This page '${originPageTitle}' is moved from '${originSpaceKey}' upon creation by Custom Listener script.")
Having an issue with this script?
Report it here