Modify "page properties" field via Adaptavist ScriptRunner
Asked by Daniel Vlad:

Hello everybody,

is there someone who can help me!

Two confluence spaces are done together.  
We use the macro "Page Properties", to define process owners for pages.

In one space is using "*Prozessverantwortlicher* " as designation, in the second space "*Prozessbevollm?chtigte*" is recorded in the "page properties" tables. So I currently have two names for "process owners". It would be very useful, if I can match this over script runner to one designation "Prozessverantwortlicher".

I got this script sample (App-Adaptavist ScriptRunner for Confluence) from Adaptavista Support. Many thanks to Lee.

```
import com.atlassian.confluence.pages.PageManager 
import com.atlassian.confluence.search.v2.ContentSearch 
import com.atlassian.confluence.search.v2.SearchManager 
import com.atlassian.confluence.search.v2.query.MacroUsageQuery
import com.atlassian.sal.api.component.ComponentLocator 
def pageManager = ComponentLocator.getComponent(PageManager) 
def searchManager = ComponentLocator.getComponent(SearchManager) 
def macroName = "details" 
def result = [] 
def search = new ContentSearch(new MacroUsageQuery(macroName), null, null, 0, 500) 
def searchResults = searchManager.search(search) 
searchResults.getAll().each { searchResult -> 
result << pageManager.getPage(searchResult.spaceKey, searchResult.displayTitle) } 
result.each{ page -> log.warn"Page: " + page }
```

<br />

I don't want to show all pages, only those from the specific space "KPKQMBAR"

```
searchResults.getAll().each { searchResult -> 
if ( searchResult.spaceKey == "KPKQMBAR"){
result << pageManager.getPage(searchResult.spaceKey, searchResult.displayTitle) 
}
}
```

When executing the code in the scriptrunner console, pages from different spaces are displayed anyway. So I'm doing something wrong.  
What should I do differently?

As soon as the results are known and I have all the pages in which the "*Prozebevollm?chtigte* " appears, I would now like to change this name to "*Prozessverantwortlicher*" via a script command.

++Here I was suggested to proceed as follows:++   
*Once you have grabbed the body content of the page, with the Page's getBodyAsString method, you can use Groovy's Replace All method to find all occurrences of a specific set of characters, in this case, the table heading* "\<th\>Prozessbevollm?chtigter\</th\>"

<br />

Unfortunately I can't get any further here. In general, are my programming ability not very good.  
How can I do this in a script?

<br />

Thanks  
Daniel


Answers:
author: Joshua Yamdogo @ Adaptavist
body: Hi Daniel,

I've just read over your support case that you previously made through our service desk support portal.

This line of code that compares the space key may be wrong:

```
searchResult.spaceKey == "KPKQMBAR"
```

The space key might be the lowercase letters "kpkqmbar" instead "KPKQMBAR", so I'd suggest just doing this:

```
searchResult.spaceKey.toUpperCase() == "KPKQMBAR"
```

Here's a script that will find the pages within the KPKQMBAR space which both use the Page Properties Macro and also contain *Prozessbevollm?chtigter.*

```
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.search.v2.ContentSearch
import com.atlassian.confluence.search.v2.SearchManager
import com.atlassian.confluence.search.v2.query.MacroUsageQuery
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.Page

def pageManager = ComponentLocator.getComponent(PageManager)
def searchManager = ComponentLocator.getComponent(SearchManager)
def macroName = "details"
def results = [] as List<Page>
def search = new ContentSearch(new MacroUsageQuery(macroName), null, null, 0, 500)
def searchResults = searchManager.search(search)
searchResults.getAll().each { searchResult ->
    if (searchResult.spaceKey.toUpperCase() == "KPKQMBAR") {
        def pageInSpace = pageManager.getPage(searchResult.spaceKey, searchResult.displayTitle)
        def stringToReplace = "<th>Prozessbevollm?chtigter</th>"
        if (pageInSpace.bodyAsString.contains(stringToReplace)) {
            results.add(pageInSpace)
        }
    }
}

results.each { page -> log.warn("Page: ${page}") }???
```

If you run this in the script console, you should get a list of pages that will need updating. If the output looks correct, you can run this next script to actually complete the find and replace.

```
import com.atlassian.confluence.core.ContentEntityManager
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.search.v2.ContentSearch
import com.atlassian.confluence.search.v2.SearchManager
import com.atlassian.confluence.search.v2.query.MacroUsageQuery
import com.atlassian.sal.api.component.ComponentLocator

def pageManager = ComponentLocator.getComponent(PageManager)
def searchManager = ComponentLocator.getComponent(SearchManager)
def contentEntityManager = ComponentLocator.getComponent(ContentEntityManager)

def macroName = "details"
def results = [] as List<Page>
def search = new ContentSearch(new MacroUsageQuery(macroName), null, null, 0, 500)
def searchResults = searchManager.search(search)

searchResults.getAll().each { searchResult ->
    if (searchResult.spaceKey.toUpperCase() == "KPKQMBAR") {
        def pageInSpace = pageManager.getPage(searchResult.spaceKey, searchResult.displayTitle)
        def stringToReplace = "Prozessbevollm?chtigter"
        if (pageInSpace.bodyAsString.contains(stringToReplace)) {
            results.add(pageInSpace)
        }
    }
}

results.each { page ->
    def replacedBodyString = page.getBodyAsString().replace("Prozessbevollm?chtigter", "Prozebevollm?chtigte")
    page.setBodyAsString(replacedBodyString)
    contentEntityManager.saveContentEntity(page, new DefaultSaveContext(true, false, true))
}
```

This will replace *Prozessbevollm?chtigter* on each page and also save the page changes using the ContentEntityManager.

Regards,

Josh

comments: author: Daniel Vlad
body: Hi Josh,  
thank you very much for your contribution and sorry for my late reply.

Unfortunately the script still doesn't work. I get the attached message.  
Tried to adapt it, unfortunately my programming knowledge is too bad : (

<br />

Maybe it's just a little something, unfortunately I'm not getting any further.

Regards,

Daniel![2020-09-24 Script Console.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/111598iA8FE8C86BFDF8515/image-size/large?v=v2&px=999 "2020-09-24 Script Console.jpg")