Add a Category to a bulk of spaces
Asked by Sasa Glisic:

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.


Answers:
author: Peter-Dave Sheehan
body: 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](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/labels/LabelManager.html#addLabel-com.atlassian.confluence.labels.Labelable-com.atlassian.confluence.labels.Label-) I can see that we need to specify a "labelable" opbject and a label. The [Labelable javadoc](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/labels/Labelable.html) shows that SpaceDescription is of of the implementing classes.

And [Space javadoc](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/spaces/Space.html) shows that it has a [getDescription](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/spaces/Space.html#getDescription--) method that returns a [spaceDescription](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/spaces/SpaceDescription.html) 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](https://docs.atlassian.com/ConfluenceServer/javadoc/7.4.6/com/atlassian/confluence/labels/Label.html), 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.

comments: author: Sasa Glisic
body: [@Peter-Dave Sheehan](/t5/user/viewprofilepage/user-id/544873) Thank you,

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


author: Jessie Wang_ScriptRunner_The Adaptavist Group
body: 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>


Tags: confluence-server

scriptrunner

server