Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,212
Community Members
 
Community Events
184
Community Groups

How to get a Confluence Page Property with Script Runner

Edited
Yves Martin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Oct 01, 2019

 Hello

For some automation tasks, I would like my groovy Script Runner code to get a page "Property" from "Page properties" macro https://confluence.atlassian.com/doc/page-properties-macro-184550024.html

Instead of parsing page content for macro, I would like to use relevant service API:

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.plugins.pageproperties.api.service.PagePropertiesService
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("confluence.extra.masterdetail")

def pagePropertiesService = ComponentLocator.getComponent(PagePropertiesService) as PagePropertiesService
def pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(pageId)
pagePropertiesService.getReportFromContent(page)

which then should allow me to use PagePropertiesMacroInstance.getPagePropertyReportRow()

But at the moment pagePropertiesService is null after ComponentLocator.getComponent

May you please point me what is wrong in that code?

Or is there an easier way to access Page Property?

Thank you in advance for your help

 

 

 

1 answer

1 vote
Yves Martin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Oct 04, 2019 • edited Jul 30, 2021

Finally found the proper way to get a plugin OSGi service reference.

Suppose you have inserted a "Page Properties" macro in Confluence page with <Page properties ID (optional)> value "metadata" (macro attribute for explicit access) and insert a row with "MyProperty" property name.

Here is how to grab "MyProperty" property's value in page:

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.plugins.pageproperties.api.service.PagePropertiesService
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("confluence.extra.masterdetail")

def pagePropertiesService = ScriptRunnerImpl.getOsgiService(PagePropertiesService) as PagePropertiesService
def pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(pageId)

pagePropertiesService.getReportFromContent(page)
.getMacroInstancesForId("metadata").find { it.getPagePropertyReportRow().containsKey("MyProperty") }
.getPagePropertyReportRow().get("MyProperty").getDetailStorageFormat()

Hope this helps

 

 

 

 

@Yves Martin , would you please clarify where to find "Document ID" and I'd appreciate if you give me advise why pagePropertiesService.getReportFromContent(page) return me the object. but pagePropertiesService.getReportFromContent(page).getMacroInstancesForId("metadata") return null.

Thanks in advance!  

Yves Martin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Jul 30, 2021

I have added details to my answer. I expect to be more clear

Thanks for the clarification and appreciate added details.
Does the script somehow depend on the confluence version?

Script looks pretty simple, but I can't get value/object neither by id pagePropertiesService.getReportFromContent(page).getMacroInstancesForId("metadata") nor pagePropertiesService.getReportFromContent(page).getAllMacroInstances(). In each cases I get nothing.

@Yves Martin : Great work! Thanks for sharing! 

@Aleksey Dzyapko : Works fine for me. I'm running it on an old Confluence Data Center v7.4.1. Remember, in Yves' script there are 3 variables:

* pageID (obvious)
* "metadata" is the string he used as a sample page property ID
* "MyProperty" is one of his properties

@Yves Martin , would you happen to have found out how to update a property or remove/add properties, too? Seems to me that service class is limited in that regard?

Like Michael Aglas likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events