Create Confluence Page based on a template when New User Created

This Event Listener will automatically create a new page from a template when a new users is created in Confluence.

Use Case: Each time a new hire is on-boarded in Confluence, a Bio page should automatically be created in a particular location in a Space where the new hire can add information about themselves including their past work history and skills.

import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.user.UserCreateEvent
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.sal.api.component.ComponentLocator
import groovy.xml.MarkupBuilder
import com.atlassian.confluence.pages.templates.PageTemplateManager
try {
    def event = event as UserCreateEvent
    def user = event.user as ConfluenceUser
    def pageManager = ComponentLocator.getComponent(PageManager)
    def spaceManager = ComponentLocator.getComponent(SpaceManager)
    def templateManager = ComponentLocator.getComponent(PageTemplateManager)
    def teamSpace = spaceManager.getSpace("HD")
    def template = templateManager.getPageTemplate(10289153)
    def parentPage = pageManager.getPage(10649613)
    assert parentPage
    def targetPage = new Page(title: "${user.fullName} Bio",
        bodyAsString: template.getContent(),
        space: teamSpace,
        parentPage: parentPage
    )
    log.warn targetPage
    pageManager.saveContentEntity(targetPage, DefaultSaveContext.DEFAULT)
    parentPage.addChild(targetPage)
    pageManager.saveContentEntity(parentPage, DefaultSaveContext.MINOR_EDIT)
}
catch (anyException) {
    log.warn("Failed to create page for new user", anyException)
}
1 Like