How to add simultaneously multiples labels with scriptrunner to a page ?
Asked by Florent R_:

Hello community,

I created a script allowing to inherit the labels of a page to all its descendants. But I find the process complicated (nested loops) and would like to simplify it. Is it possible to use another method than labelManager.addLabel to simultaneously add multiple labels to the same page?

Thank you in advance for your help !


Answers:
author: Dominic Lagger
body: Hi [@Florent R_](/t5/user/viewprofilepage/user-id/3683565)

ScriptRunner has a built-in script, where you can add, delete or rename labels in bulk.

If you want to do this in a script, then you need to hack a little bit. Let me know if you need help.

Regards, Dominic

comments: author: Florent R_
body: Thanks [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) , can you help me start to access the content of a built-in script ?


author: Dominic Lagger
body: I will try it on my instance, but because it's a bit of a hack, I need some free time :-)

Perhaps I can look at it this week, but not sure.


author: Dominic Lagger
body: Hi [@Florent R_](/t5/user/viewprofilepage/user-id/3683565)

I did some testing, but this built-in script is too hard for me. I couldn't manage it.

I stuck with the CQLSearchUtils...

But you can try, here is what I got.

```
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.ContentEntityManager
import com.atlassian.confluence.api.service.search.CQLSearchService
import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils
import com.onresolve.scriptrunner.canned.util.SimpleBuiltinScriptErrors

import com.onresolve.scriptrunner.canned.confluence.admin.model.ManageLabelsCommand 
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.user.UserManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.user.ConfluenceUser
import com.onresolve.scriptrunner.canned.confluence.admin.Actions
import com.onresolve.scriptrunner.canned.confluence.admin.Locations
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import org.apache.log4j.Level
import org.apache.log4j.Logger
import java.util.LinkedHashMap
import com.onresolve.scriptrunner.canned.confluence.admin.ManageLabels

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log.setLevel(Level.INFO)

def pageManager = ComponentLocator.getComponent(PageManager)
def userManager = ComponentLocator.getComponent(UserManager)
def labelManager = ComponentLocator.getComponent(LabelManager)

def cqlSearchService = ScriptRunnerImpl.getOsgiService(CQLSearchService)
ContentEntityManager contentEntityManager = ComponentLocator.getComponent(ContentEntityManager)
CQLSearchUtils cqlSearchUtils = new CQLSearchUtils(cqlSearchService, contentEntityManager, pageManager)

List<Long> targetPageIds = [115082958]

ManageLabels manager = new ManageLabels(labelManager, pageManager, cqlSearchUtils, userManager)
ManageLabelsCommand command = new ManageLabelsCommand()

command.setAction(Actions.ADD)
command.setLocations(Locations.CUSTOM)
command.setTargetPageIds(targetPageIds)
command.setLabels("test")
command.setNewLabelName("newlabel")

SimpleBuiltinScriptErrors errorCollection = (SimpleBuiltinScriptErrors) manager.validate(command, false)
if(errorCollection.hasAnyErrors()) {
log.warn("Error while changing Author: $errorCollection")
result = errorCollection.getErrors()
} else {
result = "OK"
}

log.debug(result)
```

Perhaps, you find the problem :)

Let me know if you could manage it.

Regards, Dominic


author: Florent R_
body: Thanks Dominic, but where did you found the source code of the build-in script ?

I will try anyway.

Have a nice day :-)


author: Dominic Lagger
body: I downloaded the JAR of the scriptrunner and decoded the class files...


author: Florent R_
body: Thanks [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) , I tried what you said but i'm too newbie in coding. I will postpone that and come back at it later.


Tags: confluence-server

label

scriptrunner

server