Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Change labels with Groovy script

Hi,

I want to run a groovy script when a button is clicked which changes the label.

We have three labels for page classification.

- Internal

- Confidential

- Public

 

I don't want that users need to change them manually. With Scriptrunner for Confluence I was easily able to create buttons in the action menu.

But I can't find an example script for Confluence.

The script should:

- Remove the existing classification label

- Add the new label

- Not change other existing labels

 

I found something similiar for Jira.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels -= 'labelToBeRemoved'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

By adding labels +='label' I believe I could add the new label

 

Can someone help? :)

2 answers

Hey Magnus,

we needed to add Labels to an existing page. And used this code:

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

static
Page addLabelsToPage(List<Label> labels, Page page) {
labels.each { Label label ->
ComponentLocator.getComponent(LabelManager).addLabel(page,label)
}
page 
}

Looks like there is a LabelManager in Confluence too ;)
Confluence Docs - LabelManager  

 

Hope that will help you with your Problem.

Greetings From Germany 

Florian

I'm interested in the answer of this as well. I am trying to add a label to a dynamically created page. I followed Florian's suggestion and looked at the Confluence LabelManager class here https://docs.atlassian.com/atlassian-confluence/6.6.0//index.html?com/atlassian/confluence/labels/LabelManager.html.

Unfortunately, when I add the following import line in my script:

import com.atlassian.confluence.label

It doesn't seem to recognize this. I am running version Confluence Server 6.13.11. How can I tell if this is available?

BTW, I tried this import inside IntelliJ and it was able to find it without a problem. So that confirms that the class exists (obviously).

Hey :)

there you should find all the available classes for your instance. 

https://docs.atlassian.com/ConfluenceServer/javadoc/6.13.0/

Form these docs:

com.atlassian.confluence.labels

Class Label

  • java.lang.Object
    • com.atlassian.core.bean.EntityObject
      • com.atlassian.confluence.labels.Label

But your issue probably comes from your import which is not correct:

// wrong
import com.atlassian.confluence.label

//right
import com.atlassian.confluence.labels.Label

 

 

Hope that can help you :) 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

New cloud apps roundup - June 2021

Since our last roundup in April, Atlassian's Marketplace Partners have added over 100 new cloud apps to the Atlassian Marketplace to help your teams work more efficiently. Let’s take a quick look a...

378 views 5 12
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