Modify "page properties" field via Adaptavist ScriptRunner

Daniel Vlad August 5, 2020

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 }


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>"


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?


Thanks
Daniel

1 answer

2 votes
Joshua Yamdogo @ Adaptavist
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.
August 6, 2020

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

Daniel Vlad September 24, 2020

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 : (


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

 

Regards,

Daniel2020-09-24 Script Console.jpg

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.4
TAGS
AUG Leaders

Atlassian Community Events