The Script Console is the place for running one-off ad hoc scripts, and for learning and experimenting with the Confluence API.

Either enter your script directly in the browser, or click the File tab, and type the path to a file. The file can be a fully-qualified path name to a .groovy file accessible to the server. If you provide a relative path name the file is resolved relative to your script roots. By default there is one script root automatically created, which is <confluence_home>/scripts/.

Read more about script roots and script plugins.

Samples

Take one of these samples and use it as a starting point for your own needs.

Enumerate all public spaces

Let’s just return all the names of all global spaces:

import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
spaceManager.allSpaces.findAll {
    it.isGlobal()
}.collect {
    it.name
}

List pages and authors across spaces

We’ll find all spaces, all pages, and their authors for all spaces in the Confluence instance. Well, we are going to limit this to three spaces and ten pages, because for large instances it’s not practical to hold all this information in a string.

We will return a Map where the keys are the Space objects, and the values are a list of Page objects.

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import groovy.xml.MarkupBuilder


def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)

def spaces = spaceManager.allSpaces.findAll {it.isGlobal()}.take(3) (1)

Map<Space, List<Page>> pagesInfo = spaces.collectEntries { space ->

    [
        (space): pageManager.getPages(space, true).findAll {
            it instanceof Page
        }.take(10) (2)
    ]
}
1 Get the first three spaces in this confluence instance
2 Get page information for up to 10 pages for each space

To view this data in a useful way we’ll format them into a table (append this code to the example above):

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder.table(class: "aui") {
    thead {
        tr {
            th("Space")
            th("Page")
            th("Author")
        }
    }

    tbody {
        pagesInfo.each { space, pages ->

            if (pages.size() > 0) {

                pages.each { page ->
                    tr {
                        th() {
                            b(space.name)
                        }
                        td(page.title)
                        td(page.creator?.name ?: "N/A")
                    }
                }
            }
        }
    }
}

return writer.toString()

On clicking Run you should see something like:

list space page

Disable Inactive Users

Test all potentially wide-ranging operations like this in a staging environment first

Let’s disable all the users who are inactive for last three months:

import com.atlassian.confluence.security.PermissionManager
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.UserManager
import groovy.time.TimeCategory

def userManager = ComponentLocator.getComponent(UserManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def loginManager = ComponentLocator.getComponent(LoginManager)
def permissionManager = ComponentLocator.getComponent(PermissionManager)

def threeMonthsBack
use(TimeCategory) {
    threeMonthsBack = 3.months.ago (1)
}
def users = userManager.users (2)

def page = users.currentPage

while (true) {

    page.each { user ->

        if (userAccessor.isDeactivated(user)) {
            return
        }

        log.debug(user)
        def userLoginInfo = loginManager.getLoginInfo(user) (3)
        def lastSuccessfulLoginDate = userLoginInfo?.lastSuccessfulLoginDate

        def toDeactivate = ! lastSuccessfulLoginDate || lastSuccessfulLoginDate < threeMonthsBack (4)
        if (toDeactivate) {
            log.warn "Found user: ${user.name} with last login date ${lastSuccessfulLoginDate}, will deactivate"
            return (5)

            if (!permissionManager.isSystemAdministrator(user)) { (6)
                log.info "Deactivating user.."
                userAccessor.deactivateUser(user)
            }
        }
    }

    if (users.onLastPage()) {
        return
    }
    else {
        users.nextPage()
    }
}
1 Select a date three months prior to today.
2 Get all the users.
3 Get login information for a specific user to get the last successful login date.
4 Compare the two dates to see whether user is inactive for last 3 months or not, or if they have never logged in
5 Remove this return statement to actually disable them, otherwise it will just print the message
6 Disable the user if the user if required and not a system administrator.
The script will take a while to execute if you have a large user base

Add/Remove User from Group

The following example shows how easily you can add or remove users from Confluence groups. In this example we will also create a group called Deactivated users - perhaps you can combine it with the example above to move all deactivated users to a group, for easy identification:

import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager

def groupManager = ComponentLocator.getComponent(GroupManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)

def inactiveGroup =  groupManager.getGroup("Deactivated users") ?: groupManager.createGroup("Deactivated users") (1)

def usersToMove = ["anuser", "u100"] (2)

usersToMove.each { username ->

    def user = userAccessor.getUserByName(username)

    if (user) {
        log.info "Removing user ${user.name} from confluence-users"
        def confUsersGroup = groupManager.getGroup("confluence-users")
        groupManager.removeMembership(confUsersGroup, user) (3)

        log.info "Add user ${user.name} to the group: 'Deactivated Users'"
        groupManager.addMembership(inactiveGroup, user) (4)
    }
}
1 Create a new user group "Inactive Users" if it does not exists
2 Hard-coded list of users to modify
3 Remove group membership for the user
4 Now add the user to the group we have created in step 1

For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.

Ask a question about ScriptRunner for JIRA, for for Bitbucket Server, or for Confluence.