Example: Return Page Information

You can create a macro to pull the page information (like page title, the page creator, the parent page, and the space) of Confluence content onto a Confluence page.

Create the macro

  1. Select the Settings cog in the top right-hand corner of the screen. 
  2. Select Macros under ScriptRunner
  3. Select Create Custom Macro
  4. Enter a Name to identify the Macro, like Page Information
  5. Enter an optional Description, like Get the current page title, the page creator, the parent page, and the space
  6. Select Enabled to allow the macro to be added to pages. 
  7. Select None for Body Type. 
  8. Pick Block for Output Type
  9. Enter the following script into the Script to Execute field: 

    This script uses the response status code for error checking.

    groovy
    //ERROR CHECKING USING THE RESPONSE STATUS //Variables scoped and default value assigned def pageInfo String pageLink, spaceLink, ownerLink, parentLink pageLink = spaceLink = ownerLink = parentLink = "Not available" //Catch-all error management //Use Implicit Parameters to create Page and Space links pageLink = "<ac:link><ri:content-entity ri:content-id='${parameters.pageId}'/></ac:link>" spaceLink = "<ac:link><ri:space ri:space-key='${parameters.spaceKey}'/></ac:link>" //Get page info via REST using Unirest pageInfo = get("/wiki/api/v2/pages/${parameters.pageId}") .header("Accept", "application/json") .asObject(Map) if (pageInfo.status == 200) { if (pageInfo.body.ownerId) { //Space overview pages return null for owner ID //Explicit null check - if (pageInfo.body.ownerId != null) - is not necessary in Groovy ownerLink = "<ac:link><ri:user ri:account-id='${pageInfo.body.ownerId}'/></ac:link>" } else { ownerLink = "This page does not have an owner." //Degrade gracefully } //Get parent info if available (Space Overview pages do not have a parent) if (pageInfo.body.parentId) { parentLink = "<ac:link><ri:content-entity ri:content-id='${pageInfo.body.parentId}'/></ac:link>" } else { parentLink = "There is no parent for this page." } } else { logger.info("pageInfo GET error: " + pageInfo.statusText) // Not very informative // + pageInfo.body.errors.code + pageInfo.body.errors.title would be better // or + pageInfo.body.errors return "<p>Page Information is not available currently. Pleasee try \ refreshing the page or contact an administrator if the problem persists.</p>" } return """ <h4>Links</h4> <p><strong>Current Page</strong>: ${pageLink}</p> <p><strong>Page owner</strong>: ${ownerLink}</p> <p><strong>Parent Page</strong>: ${parentLink}</p> <p><strong>Space</strong>: ${spaceLink}</p> """
  10. Select Save.

Results

The macro appears on the main Macros page:

Users in your instance can now add it to Confluence pages. When it's added and the page is published, it appears like this:

On this page