Skip to end of metadata
Go to start of metadata

Creating a new page from a form submission is useful if form responses require follow up or collaborate action, for example, if a user submits an internal feature request or events proposal. If you have Script Runner for Confluence installed you can run a script on the “FormSubmitEvent” that creates a new Confluence page when a form is submitted. 


Instructions

  1. Create a new form that includes input fields to collect a specified space key, page title and page content. You can include additional fields such as a parent page or Forms Attachment macro if needed.



  2. Configure the event listener for FormsSubmitEvent, where the spaceKeypageTitle, and pageContent correspond to the name parameter defined for the macros in your form. When the form is submitted, a new page is created with the values entered by the user.



import com.atlassian.confluence.core.DefaultSaveContext
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.xwork.FileUploadUtils
import groovy.json.JsonSlurper
 
try {
    String event = ((HashMap<String, String>) binding.variables).get("event").structuredValue
    Map<String, String> data = new JsonSlurper().parseText(event)
 
    List<FileUploadUtils.UploadedFile> uploadedFiles = ((HashMap<String, String>) binding.variables).get("event").uploadedFiles
 
    def pageManager = ComponentLocator.getComponent(PageManager)
    def spaceManager = ComponentLocator.getComponent(SpaceManager)
 
    def spaceKey = data.get("spaceKey")[0]
    def pageTitle = data.get("pageTitle")[0]
    def pageContent = data.get("pageContent")[0]
 
    def targetPage = new Page(
        space: spaceManager.getSpace(spaceKey),
        title: pageTitle,
        bodyAsString: pageContent,
    )
 
    pageManager.saveContentEntity(targetPage, DefaultSaveContext.DEFAULT)
 
} catch (anyException) {
    log.warn("Failed to create page for new user", anyException)
}

FormsSubmitEvent in Action

Using our example script and form, when the form is submitted, a new page is created in the DW (Documentation) space in Confluence titled Who is in/out, with the Request details displayed in the page body. Users can then comment on, share, update, or action this page as needed.






  • No labels