Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,301,230
Community Members
 
Community Events
165
Community Groups

Need help with a script to find and replace text in multiple confluence pages

Hi,

I have been assigned a task to find and replace few words in confluence pages and I came across script runner as a very useful tool. I will be very thankful if anyone in this group can help me with a script that will help me to accomplish my task of finding and replacing text at the page level in confluence.
Thanks in advance,
Regards,
M J

2 answers

1 accepted

0 votes
Answer accepted
Kishan Sharma Community Leader Sep 18, 2021

Hi @Jagadeesh Welcome to the Atlassian Community!

This blog on scriptrunner site already have a script that you can download and modify as per your requirement. The example script works on a single space, but you can modify the code to loop through all your confluence spaces and its pages and perform search/replace as per your use case.

Hi Kishan,

Thank you very much for your quick reply. Yes, I am using the sample script shared by the Script runner trial version tool, but it is throwing errors and I need someone who has already used this script to help me in debugging or modifying the current script. 

The sample script shows how to modify within a table in confluence page, I want to modify text within a page in confluence. I am not sure what code elements to use. if you have an idea how to modify the script, please do message me at this mail id

jmadineni@billtrust.com. I will be very thankful for helping me out.

Regards,

M Jags

Kishan Sharma Community Leader Sep 20, 2021

Hi Jagadeesh,

I will also suggest posting this question to Atlassian Developer Community so that you will get better solutions from the developers working on such scripting activities. I will also post here in case I able to get this to work.

Hi Kishan,

Thanks for the suggestion. Sure, I will post in the developer community too. I have another 10 days before the trial version expires. In the meanwhile, if you know any groovy script developers please suggest their mail ids. I will try connecting with them.

Hi Jagadeesh,

Here's what your script might look like if you wanted to find and replace text anywhere on particular Confluence pages:

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import org.jsoup.Jsoup

def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)

def targetSpace = spaceManager.getSpace("SAL")

pageManager.getPages(targetSpace, true).each { page ->
log.debug "Inspecting page ${page.title}"
def body = page.bodyContent.body
body = body.replace('Human Resources', 'HR')
pageManager.saveNewVersion(page) { pageObject ->
pageObject.setBodyAsString(body)
}
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

416 views 2 13
Read article

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