I am trying to manipulate a Confluence page via Adaptavist ScriptRunner for Confluence and the Java API. Changing the content of the page works as expected but the change ist not correctly reflected in the page history - the content is overwritten with the new version the version count is increased but the old version is simply lost.
Here's my setup to reproduce this (Confluence Server 7.0.5, Scriptrunner 5.9.0)
- I create a page with title "Test for Update" with one paragraph text "Content"
- Looking at the page history it shows version v.1 created 9:07
- I run the script in Scriptrunner console (see code below)
- Opening the page shows the correctly manipulated content - all seems fine
- Looking now at the page history again it shows version v.2. (correct) with the same timestamp 9:07 altough the script ran 9:08 and version v.1 is simply lost - no way to track the changes or revert to v.1
That's my script code:
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.core.Modification
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.nodes.Document
PageManager pageman = ComponentLocator.getComponent(PageManager)
def page = pageman.getPage("scriptrunner", "Testpage for Update")
def src=page.getBodyAsString()
def doc = Jsoup.parse(src)
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
def element = doc.getElementsContainingText("Content")
if (element.size()>0) {
element[0].text("Updated Content")
pageman.saveNewVersion(page, new Modification<Page>() {
public void modify(Page p) {
p.version = p.version + 1
p.bodyAsString = doc.body().toString()
}
}, DefaultSaveContext.MINOR_EDIT);
}
else {
log.warn("no content found")
}
Some screenshots to document the behaviour:
1.) v.1 of page with page history:


2.) After running script - correctly modified - incorrect page history, v.1 lost, timestamp wrong:


I tried using
pageman.saveContentEntity(page, DefaultSaveContext.MINOR_EDIT)
instead of
pageman.saveNewVersion(page, new Modification<Page>() {
public void modify(Page p) {
p.version++
p.bodyAsString = doc.body().toString()
}
}, DefaultSaveContext.MINOR_EDIT);leading to the same result.
So what's the correct why to create a new version of the page?
Any help and suggestions highly appreciated.
Kurt