Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to access ContentService or ContentPropertyService via ScriptRunner

Jeremy Goodwin
Contributor
April 6, 2021

Hi,

I'm hoping someone can suggest how to achieve this. I want to use the ContentProperty to store some values for a Confluence page, but can't get a handle on either the ContentService or the ContentPropertyService.

 

I'm using Confluence 7.10.2 and ScriptRunner 6.21.0.

I can get an instance of both services via

@Field ContentService contentService = ComponentLocator.getComponent(ContentService, 'apiContentService');

@Field ContentPropertyService contentPropertyService = ComponentLocator.getComponent ContentPropertyService;

using this, I can successfully create and add a new property via contentPropertyService.create( myNewProperty )

however I'm unable to use .find to access the ContentPropertyFinder as

ContentId contentId = ContentId.of(pageId)

def found = contentPropertyService.find().withContentId(contentId).fetchOne();

groovy.lang.MissingMethodException: No signature of method:  com.sun.proxy.$Proxy260.withContentId() is applicable for argument types: (com.atlassian.confluence.api.model.content.id.ContentId) values: [ContentId[id=19071593}]

And I have a similar issue with ContentService

def page = contentService.find().withId(ContentId.valueOf(pageId as String)).fetchOneOrNull();

which crashes out in a similar manner

groovy.lang.MissingMethodException: No signature of method: com.sun.proxy.$Proxy217.withId() is applicable for argument types: (com.atlassian.confluence.api.model.content.id.ContentId) values: [ContentId{id=19071593}].

Possible solutions: with(groovy.lang.Closure), with(boolean, groovy.lang.Closure), wait(), find(), wait(long), find([Lcom.atlassian.confluence.api.model.Expansion;)

If I try and call .find with null or an Expansion, I get

java.lang.IllegalArgumentException: interface com.atlassian.confluence.api.service.content.ContentService$ContentFinder is not visible from class loader

 

I believe my issue is due to Spring/autowire and Groovy's proxy getting in the way of each other but I'm at a loss as to how to resolve this.

1 answer

1 accepted

1 vote
Answer accepted
Jeremy Goodwin
Contributor
April 6, 2021

For anyone else who has this issue, the answer appears to be to explicitly set the Expansions, which where trial-and-error.

 

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

found.get().value

does the trick

Michael Aglas
Contributor
May 12, 2022

what is propertyKey? is it the space key?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events