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

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

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.

Suggest an answer

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

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

723 views 7 24
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