How to get a Confluence Page Property with Script Runner

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.
October 1, 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.
October 4, 2019

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

 

 

 

 

Aleksey Dzyapko July 30, 2021

@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.
July 30, 2021

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

Aleksey Dzyapko August 4, 2021

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.

Steffen John April 21, 2022

@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

Steffen John April 22, 2022

@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