# Update content on multiple Confluence pages in one go

**URL:** <https://forum.library.adaptavist.com/t/update-content-on-multiple-confluence-pages-in-one-go/36>\
**Category:** Scripts\
**Tags:** confluence, data-center\
**Created:** [March 4, 2022, 6:56pm UTC](https://forum.library.adaptavist.com/t/update-content-on-multiple-confluence-pages-in-one-go/36 "2022-03-04T18:56:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcarter](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/jcarter/32/16_2.png) [@jcarter](https://forum.library.adaptavist.com/u/jcarter)\
**Post date:** [March 4, 2022, 6:56pm UTC](https://forum.library.adaptavist.com/t/update-content-on-multiple-confluence-pages-in-one-go/36/1 "2022-03-04T18:56:24Z")

</div>

> If you’re looking to update Macros in pages, give a look to our [Update Macro](https://docs.adaptavist.com/sr4c/latest/features/built-in-scripts/confluence-administration-built-in-scripts/update-macro) feature!

One question we get fairly frequently is how to bulk update page content using ScriptRunner.

One of the complexities of doing so is dealing with the [Confluence storage format](https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html), which is an XHTML-based document. Doing simple string replacements on HTML using familiar tools like regular expressions can be [a bit fraught](https://stackoverflow.com/a/1732454/1524502), but that doesn’t mean we can’t easily get things done. The convenience of the Groovy programming language and powerful libraries like [jsoup](https://jsoup.org/) combine to make these sort of changes feasible.

As a simple example, suppose that we have several different pages in our space that each contain a table. That table has a header column with some placeholder text that we want to change.

 ![example-table](https://us1.discourse-cdn.com/flex019/uploads/adaptavistlibrary/original/1X/ddf03bbf2885f63d9a68be0d697aa75e237d7285.png)

Suppose further that all of these pages are children of a known parent page. A script like the one below can be run from the [Script Console](https://docs.adaptavist.com/sr4c/latest/features/script-console) to perform the update, while leaving placeholder text that’s _not_ in a table header alone.

```auto
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import org.jsoup.Jsoup

def pageManager = ComponentLocator.getComponent(PageManager)
def rootPage = pageManager.getPage('DS', 'Welcome to Confluence') //You can change this line to point to the right parent page.
rootPage.children.each { page ->
    log.debug "Inspecting page ${page.title}"
    def body = page.bodyContent.body
    def parsedBody = Jsoup.parse(body)
    def tableHeaderWithPlaceholder = parsedBody.select('th:contains(placeholdertext)')
    if (!tableHeaderWithPlaceholder.empty) {
        log.debug "Found table header with placeholder text: ${tableHeaderWithPlaceholder}"
        pageManager.saveNewVersion(page) { pageObject ->
            tableHeaderWithPlaceholder.html("${page.creator.fullName}'s Column")
            pageObject.setBodyAsString(parsedBody.toString())
        }
    }
}

```

That transforms the table as you might expect to something like this:

 ![transformed-table](https://us1.discourse-cdn.com/flex019/uploads/adaptavistlibrary/original/1X/9c6af5757f068f28e8f2dfd56a1d96c97329eb81.png)

Of course, the above script can be updated for more complex use cases, such as iterating through the results of a CQL search, changing content besides placeholders, and so on.

Happy scripting!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/adaptavistlibrary/original/1X/ceb18adf14fa58b4c6de03aa69ada8b07359620c.png) [@system](https://forum.library.adaptavist.com/u/system)\
**Post date:** [August 31, 2022, 6:56pm UTC](https://forum.library.adaptavist.com/t/update-content-on-multiple-confluence-pages-in-one-go/36/2 "2022-08-31T18:56:39Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
