Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Transforming body content as well as page title using ScriptRunner for Confluence

Confluence administrators can copy page trees using a built-in script, but that script can do more than just copy the pages as-is. The blank at the end for transforming the title can actually do much more to enhance the new pages.

For example, suppose you wanted to replace all occurrences of the term PLACEHOLDER in a page with a replacement term.

import com.atlassian.confluence.pages.AbstractPage

titleTransform = { AbstractPage p ->
    p.title = "Copy of " + p.title
    //Straightforward string replacements will work
    p.bodyAsString = p.bodyAsString.replaceAll('PLACEHOLDER', 'REPLACEMENT')
}

But we can affect more than just the plain text of the page. For example, suppose that you wanted to change user mentions to point from one user to another in the new copy.

import com.atlassian.confluence.pages.AbstractPage
import com.atlassian.sal.api.user.UserManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def userManager = ScriptRunnerImpl.scriptRunner.getBean(UserManager)
def placeholderuser = userManager.getUserProfile('placeholderuser')
def beatrice = userManager.getUserProfile('beatrice')

titleTransform = { AbstractPage p ->
    p.title = "${beatrice.fullName}'s Copy of " + p.title
    p.bodyAsString = p.bodyAsString.replaceAll(placeholderuser.userKey.toString(), beatrice.userKey.toString())
}

Of course, with great power comes great complexity. If the placeholder you use is simple enough, it may show up in one of the subpages in a context that you don't want to replace. Be mindful of that if you're building out a scaffolding of pages as template.

Depending on what you want to find & replace, it may be helpful to look at the storage format of the Confluence page.

0 comments

Comment

Log in or Sign up to comment
TAGS
Community showcase
Posted in Confluence

What do you think is the most *delightful* Confluence feature? Comment for a prize!

- Create your own custom emoji 🔥 - "Shake for Feedback" on mobile 📱 - An endless supply of GIFs via GIPHY 🤩 Is there anything quite as nice as a pleasant surprise? Comment below with what...

431 views 23 9
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you