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

Getting macros from page properties via Java/Groovy API

Tom Maughan October 13, 2020

I'm trying to write a bit of code that pulls the @ user mention details from a specific line in the page properties of a particular page.

I'm using the PagePropertiesService with getReportFromContent(page) and then getAllMacroInstances().

I can then iterate through list returned by getAllMacroInstances().   I can locate the page property am looking for BUT getDetailStorageFormat() returns a blank string.

If I use a page property that is just a string typed into the page rather than a macro I get the expected text.   It appears that getDetailStorageFormat() does not work when there is a macro involved.

It also appears that the PagePropertiesService is an undocumented API.

I borrowed quite a bit of code from here:

https://community.atlassian.com/t5/Confluence-questions/How-can-I-filter-my-Confluence-search-by-page-properties/qaq-p/953887

Has anyone else tried this?

1 answer

0 votes
Ken McClean
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.
May 2, 2023

I'm not 100% sure what you were going for with your approach to the macros, but you can search the body of a given page for an @ mention with this script

@-mentions take the form of  ri:userkey="8aab818c73caedf00173caf477050000", where the string of characters is the user ID.    Therefor if we parse the body of a page for that string, we'll turn up any @-mentions in the system.

Worth noting is the fact that while you can return the body of a page without the wiki markup, it doesn't include the @.  So you'd just be searching for the name of the user, which is ultimately less specific.


 

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page
def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def spaces = spaceManager.getAllSpaces()
 
def targetMention = 'ri:userkey="8aab818c73caedf00173caf477050000"'
 
spaces.each{spaceObj ->
//Iterate through all spaces in the system
 def pages = pageManager.getPages(spaceObj, false)
  pages.each{page ->
 //iterate through all pages in each space
if(page.getBodyAsString().contains("${userkey}") && page.version == page.latestVersion.version){
//Check if the page body contains the target, and ensure that the page we're checking is the latest version of the page

log.warn("${page.title} contains an @ mention of the user")}
}
  }

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events