How to use expansions on ContentPropertyService?

Hi,

I’m using ScriptRunner to create some properties for a page, which works ok. What I can’t seem to do is use .find to return them as Groovy’s proxy class blocks .find unless I explicitly set an Expansion; however I can’t figure out the correct expansions to set.

@Field ContentPropertyService contentPropertyService = ComponentLocator.getComponent ContentPropertyService;

ContentId pageId = ContentId.of(Long.valueOf('19071593');
String myKey = 'my-key';
JsonContentProperty newProperty = JsonContentProperty.builder()
   .content(Reference.to(content.builder().id(pageId).build()))
   .key(myKey)
   .value(new JsonString('{\'when\': \'2021-04-08\'}'))
   .build()
contentPropertyService.create(newProperty)

works, and I can see the property exists by hitting the rest endpoint
/rest/api/content/19071593/property/my-key

{"id":"19071593", "key":"my-key", "value":{'when': '2021-04-08'}, "version...}

but if I try getting the content from the service instead of the REST api, I get cannot call get() on collapsed object

def found = contentPropertyService.find(new Expansion(JsonContentProperty.Expansions.CONTENT), new Expansion('property'))
   .withPropertyKey('my-key')
   .withContentId(pageId)
   .fetch()

 log.warn "found: ${found.get()"

So what are the correct expansions to pass in? I can’t do contentPropertyService.find() because Groovy gets upset as it creates a proxy that doesn’t have access to the finder interface, so I believe I’ve got to use expansion

Answering myself in case anyone else has this issue

def found = contentPropertyService.find(new Expansion('content'), new Expansion('version')
   .withPropertyKey('my-key')
   .withContentId(pageId)
   .fetch()
log.warn found.get().value

gets the values.

I don’t suppose the Java APIs can have some more involved examples so that people don’t spend a day trying to use the ‘new’ APIs?

1 Like

Thank you so much. Yes, it seems Atlassian didn’t document at all the use of ContentService or how to update the body of a page.

The thing to remember is, it works like in the REST API. Imagine you want to get the storage format of a page, and you would write the URL with ?expand=body.storage - You would get the same with:

contentService.find(
    new Expansion("body", new Expansions(new Expansion("storage")))
)
.withId(ContentId.of(pageId))
.fetch()
.get().getBody()

@aragot , I’ve recently found that all these new API services can be easily loaded via the ScriptRunner @PluginModule annotation, making usage a little easier.

@PluginModule
ContentService contentService

for example, however you must using the Expansions on the find else Groovy’s proxy class loader goes haywire. Unfortunately the REST API documentation doesn’t give comprehensive information on expansion options; some Services contain a static default set, but it appears for the majority, it’s a voyage of discovery on the REST API version first…

Your note on how to use the compound Expansions is very useful - I’d not noted the Expansions class in the Javadoc.