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,365,271
Community Members
 
Community Events
168
Community Groups

How to get a Confluence Page Property with Script Runner

Edited

 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

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!  

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?

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events