I have a request to Space wide update the hyperlinks for a particular URL. I have a script to replace the text but it did not touch the URL in the hyperlink.
Hi @gaven
Your script here is almost perfect, just one change needs to be made.
In this block:
if (pageBody.contains(oldText)){
pageBody = pageBody.replace(oldText, newText)
page.getBodyAsString()
pageManager.saveContentEntity(page, DefaultSaveContext.DEFAULT)
}
The first line of your If Statement is creating the new body, by replacing any of the old text with the new text. This is spot on. The line after that:
page.getBodyAsString()
Is just getting the existing page again. What we really need to do is change the body of the page to the new value you just created. So, if we change this line to:
page.setBodyAsString(pageBody)
This updates the page with the new body that you defined, with the replaced urls. So, your whole script will look like this:
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
def oldText = 'http://'
def newText = 'https://'
def targetSpaceId = 'XXXX'
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def targetSpace = spaceManager.getSpace(targetSpaceId)
def spacePages = pageManager.getPages(targetSpace, true)
spacePages.each{page ->
def pageBody = page.getBodyAsString()
if (pageBody.contains(oldText)){
pageBody = pageBody.replace(oldText, newText)
page.setBodyAsString(pageBody)
pageManager.saveContentEntity(page, DefaultSaveContext.DEFAULT)
}
}
Could you give this a go and let me know if it works for you?
Kind regards,
Bobby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.