Add a Category to a bulk of spaces

Sasa Glisic July 7, 2021

I have a request to add a certain category/label to a bunch of Spaces. I need a script for that since there are more than 100 Spaces. I manage to get Category from one space, but I don't know how to add a category through Groovy script.

Here are the code for getting a Category, note maybe some imports are obsolete since I was trying different things

//start

import com.atlassian.confluence.pages.Page
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.spaces.SpaceManager

def labelManager = ComponentLocator.getComponent(LabelManager)

def newLabel = "myLabel"

return labelManager.getTeamLabelsForSpace("spaceCode")

//end

This code returns a label, but what method adds it. I found addLabel method but don't know how to use it. 

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2021

I'm expecting to have to do something similar soon... so I took some time to figure out how to do it.

Base on the labelManager javadoc I can see that we need to specify a "labelable" opbject and a label. The Labelable javadoc shows that SpaceDescription is of of the implementing classes.

And Space javadoc shows that it has a getDescription method that returns a spaceDescription object.

So putting all this together I tried:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.spaces.SpaceManager

def labelManager = ComponentLocator.getComponent(LabelManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)

def newLabel = new Label("mylabel")
def space = spaceManager.getSpace('QMS')
labelManager.addLabel(space.description, newLabel)

But that didn't work. I manually added the mylabel to my test space via the web UI and ran this sql query:

Select 
c_l.ID, c_l.CONTENTID, u.username As Owner, c_l.LABELABLEID, c_l.LABELABLETYPE, l.NAME, l.NAMESPACE
From
confldb_test3.CONTENT_LABEL c_l Inner Join
confldb_test3.LABEL l On c_l.LABELID = l.LABELID Inner Join
confldb_test3.user_mapping u On c_l.OWNER = u.user_key
Where
l.NAME = 'mylabel'

And I could see both labels (the one added by the script and the manual one), but with different namespaces.

+-----------+-----------+-------+-------------+---------------+---------+-----------+
| ID | CONTENTID | Owner | LABELABLEID | LABELABLETYPE | NAME | NAMESPACE |
+-----------+-----------+-------+-------------+---------------+---------+-----------+
| 269975553 | 39029345 | p6s | 39029345 | CONTENT | mylabel | global |
| 269975554 | 39029345 | p6s | 39029345 | CONTENT | mylabel | team |
+-----------+-----------+-------+-------------+---------------+---------+-----------+

Looking at the Label javadoc, I see that there is a constructor that lets us specify the namespace as a string. 

So, I made a little adjustment to the script:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.spaces.SpaceManager

def labelManager = ComponentLocator.getComponent(LabelManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)

def newLabel = new Label("mylabel2", 'team')
def space = spaceManager.getSpace('QMS')
labelManager.addLabel(space.description, newLabel)

And it worked!

I hope that sharing my exploration process with the javadocs will help you solve similar future puzzles.

Sasa Glisic July 8, 2021

@Peter-Dave Sheehan Thank you,

that's just what I was looking for, both code and exploration proccess

0 votes
Jessie Wang_ScriptRunner_The Adaptavist Group
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 28, 2022

Hi all, this script is now available in our script library for ScriptRunner for Confluence Server/DC (tested by our engineers).

Feel free to copy or customise it as you wish

https://library.adaptavist.com/entity/add-labels-to-spaces-in-bulk

Suggest an answer

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

Atlassian Community Events