Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Add Labels To Spaces In Bulk

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.confluence.labels.SpaceLabelManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.Space
import com.onresolve.scriptrunner.parameters.annotation.SpacePicker

@SpacePicker(label = 'Spaces', description = 'Select the spaces to apply labels to', multiple = true)
List<Space> spaces
assert spaces

//Edit the Label names to meet your need
def labelNames = ['label1', 'label2']

SpaceLabelManager spaceLabelManager = ComponentLocator.getComponent(SpaceLabelManager)
def errors = [] as Set

spaces.each { space ->
    labelNames.each { labelName ->
        try {
            spaceLabelManager.addLabel(space, labelName)
        } catch (IllegalArgumentException e) {
            errors.add(e.message)
        }
    }
}

if (errors.size() == 0) {
    log.info("All labels listed have been added to the selected spaces.")
} else {
    log.error("All labels listed have been added to the selected spaces except the following error(s).")
    errors.each { log.error(it) }
}
Having an issue with this script?
Report it here