Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Replacing links URL Text using Script Runner

gaven November 21, 2023

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. 

 

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.getBodyAsString()
        pageManager.saveContentEntity(page, DefaultSaveContext.DEFAULT)
       
    }
}
Is there a way for it to make those changes?
My assumption is that page.getBodyAsString() is not quite right or it needs to be tweaked to pull the storage format.

1 answer

1 accepted

2 votes
Answer accepted
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2023

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

gaven November 22, 2023

Thanks for that. It did work as far as I can tell. I have now added logging to ensure it will work.

Like Bobby Bailey likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events