Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Add a Category to a bulk of spaces

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. 

1 answer

1 accepted

0 votes
Answer accepted

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.

@Peter-Dave Sheehan Thank you,

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Confluence

🏑 Atlympic Event: Confluence

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

172 views 6 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you