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

How to add simultaneously multiples labels with scriptrunner to a page ?

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 !

1 answer

0 votes
Dominic Lagger Community Leader Mar 12, 2021

Hi @Florent R_ 

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

Thanks @Dominic Lagger , can you help me start to access the content of a built-in script ?

Dominic Lagger Community Leader Mar 16, 2021

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.

Dominic Lagger Community Leader Mar 17, 2021

Hi @Florent R_ 

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

Thanks Dominic, but where did you found the source code of the build-in script ?

I will try anyway.

Have a nice day :-)

Dominic Lagger Community Leader Mar 17, 2021

I downloaded the JAR of the scriptrunner and decoded the class files... 

Thanks @Dominic Lagger , I tried what you said but i'm too newbie in coding. I will postpone that and come back at it later.

Suggest an answer

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

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,474 views 7 29
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