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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,414,261
Community Members
 
Community Events
170
Community Groups

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. 

2 answers

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

0 votes

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

Atlassian Community Events