Replacing links URL Text using Script Runner
Asked by gaven:

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?  
I am not great at scripting and mostly took this from <https://docs.adaptavist.com/sr4c/8.16.0/training/course-introduction-to-scripting-in-scriptrunner-for-confluence/3-1-video-introduction-to-groovy-basics-for-confluence>  
My assumption is that page.getBodyAsString() is not quite right or it needs to be tweaked to pull the storage format.


Answers:
author: Robert Bailey
body: Hi [@gaven](/t5/user/viewprofilepage/user-id/5311392)   

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)

    }
```

<br />

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()
```

<br />

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)
```

<br />

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)

    }
}?
```

<br />

Could you give this a go and let me know if it works for you?  

Kind regards,  

Bobby

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


Tags: confluence-server

server