Automatic page numbering in Atlassian Confluence

Atlassian Confluence is a very flexible Wiki system. By default it is not possible to force a specified structure. Each user is able to create pages and name them as she/he wants to. We had the requirement to give all pages created by users in a special space a prefix with automatic page numbering in Atlassian Confluence. The Add-On Adaptavist Scriptrunner for Confluence will be necessary to achieve this requirement in our approach.

Adaptavist Scriptrunner includes a “Create Page” macro which has the possibility to create pages with variables in the title. There is a variable called $ident. If you place it on a page with the parameter $ident in the title it creates a page with incrementing numbers each time you click it. But if the user now changes this name it does not get updated any more and the next created page gets a number what should already be in use. This was not the solution we were looking for. However, Scriptrunner is a great tool – we will use the “Create Page” macro on the homepage to create pages but without the $ident variable.

Create Page Macro by Adaptavist Scriptrunner for Confluence

Place the macro onto the Confluence page where you want the user to start creating new pages. One necessary parameter in the macro will be “Labels“. In this example it is set to “apn“. This will add the label “apn” onto each page that we create with the button. Also set the parameter “Target Mode” to “Edit“. This is necessary to identify which pages should get an automatic page numbering.

Automatic page numbering in Atlassian Confluence - Add Create Page Macro

Event Listener to achieve automatic page numbering

Adaptavist Scriptrunner for Confluence also offers to possibility to create Event Listeners. We will use this to add or check the page title. Everytime it gets updated it will execute and add the prefix if it is missing. The defined prefix will be stored in the page’s content properties. The current incremental number will be stored in the home page’s content properties of the space.

  1. Open “General Configuration” and navigate to “Event Listeners
  2. Click “Create Listeners” -> Custom event listener
  3. Add “PageUpdateEvent” to Events parameter
  4. Copy and paste the following code into “Inline Script” section or save it as file and link it – then save the event listener
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.ContentPropertyManager
import com.atlassian.sal.api.component.ComponentLocator


def contentPropertyManager = ComponentLocator.getComponent(ContentPropertyManager)

def event = event as PageEvent
def page = event.getPage()
def pageLabels = page.getLabels()
def space = page.getSpace()
def homePage = space.getHomePage()

// Page created or updated inside of defined space
if (space.getKey() == "APN"){
    // Check if the page has one of the defined labels (apn, something) and define the PREFIX
    def pageNrPrefix = null

    if (pageLabels.toString().contains("apn")){
        pageNrPrefix = "APN"
    if (pageLabels.toString().contains("something")){
        pageNrPrefix = "SOMETHING"
    // If the defined label is set a prefix is defined and we can proceed
    if (pageNrPrefix){
        // Get page numbering of current page
        def pageNr = null
        def pageNrString = null
        if (contentPropertyManager.getStringProperty(page, "pageNr")){
            pageNr = contentPropertyManager.getStringProperty(page, "pageNr").toInteger()
            pageNrString = contentPropertyManager.getStringProperty(page, "pageNrString")
        // Check if page count is already stored in homepage - if not set it
        if (!contentPropertyManager.getStringProperty(homePage, "${pageNrPrefix}PageCount")){
        	contentPropertyManager.setStringProperty(homePage, "${pageNrPrefix}PageCount", "0")    
        // Calculate next page number if it is a new page
        if (!pageNr){
            // Get next number and save it to homepage
            def lastPageNr = contentPropertyManager.getStringProperty(homePage, "${pageNrPrefix}PageCount").toInteger()
            def nextPageNr = lastPageNr + 1
            pageNr = nextPageNr
            pageNrString = "${pageNrPrefix}-${pageNr.toString()}"
            contentPropertyManager.setStringProperty(homePage, "${pageNrPrefix}PageCount", nextPageNr.toString())
            // Save Page Number to current page
            contentPropertyManager.setStringProperty(page, "pageNr", pageNr.toString())
            contentPropertyManager.setStringProperty(page, "pageNrString", pageNrString)

        // Update Page Title if Page Number with prefix is not in page title
        if (page.getTitle().indexOf(pageNrString.toString()) != 0){
            page.setTitle(pageNrString.toString() + " " + page.getTitle())

View code in Github

If you open the homepage of your space again and create a new page with the macro what we have placed before, it will get an automatic page numbering in the title called APN-1. The next page will receive APN-2 and so on.

Automatic page numbering in Atlassian Confluence - See the result

If anybody removes the prefix from the page title, the event listener will add it again.

The space key on which the listener executes is defined in line 16 of the script. Adjust it to your needs.

The label what you use when you create a new page is defined in line 21 and the prefix of the page title in line 22. Also adjust these 2 strings.

The solution supports multiple different prefixes. An example for it are lines 24-26. Just multiply them as many times as you need different prefixes. If one is enough just remove them.

Leave a Reply