Trying to create Script CQL Function to find related content from the labels on the current page. Edited

I am using ScriptRunner for Confluence and am attempting to expand the "Search Page By Label" Custom CQL Function example to be able to pull all the labels from the current page (where the CQL function is called) and return the related pages.

My ultimate goal is to not have to replicate adding labels on a page twice; once on the page itself and then once again in a related content by label macro. I would lie to simply add the CQL Search macro to my custom page template and allow the CQL function to provide the related content results when the page is loaded.

I have attempted to find a solution through both Atlassian and Adaptavist sites but have had no luck thus far. Also, to be completely honest, I am not familiar with groovy.

I have the following working if I pass in a single label from the CQL Search macro:

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
String label = params[0]
String spaceKey = "OCCIKB"
def space = spaceManager.getSpace(spaceKey)
def ids = []

for (Page page : pageManager.getPages(space, true)) {
for (Label pageLabel : page.getLabels()) {
if (pageLabel.getName().equalsIgnoreCase(label)) {
ids << String.valueOf(page.getId())
}
}
}
return ids

What I would like to do is change the script to pull the list of labels from the current page and iterate over the list of labels and return the page ids of the pages that contain matching labels.

I have modified the above script to this:

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
String spaceKey = "OCCIKB"
def space = spaceManager.getSpace(spaceKey)
def ids = []

for (Page page : pageManager.getPages(space, true)) {
for (Label myPageLabels : Page.getLabels()) {
String myPageLabel = myPageLabels
for (Label pageLabel : page.getLabels()) {
if (pageLabel.getName().equalsIgnoreCase(myPageLabel)) {
ids << String.valueOf(page.getId())
}
}
}
}
return ids

but am getting the following error returned:

Error rendering macro 'cql-search' : No signature of method: static com.atlassian.confluence.pages.Page.getLabels() is applicable for argument types: () values: [] Possible solutions: getLabels(), getLabelUtil(), getLabellings(), getParent(), getTeamLabels(com.atlassian.user.User), getClass()

I would appreciate any assistance in helping me understand how I can pull the labels attached to the current page.

0 answers

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

71 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you