HOWTO? use ContentService.find Java API with ScriptRunner (Groovy)

The API documentation for PageManager states that all the .find functions are deprecated and we should instead use ContentService.find, however using ScriptRunner it fails with ContentService$ContentFinder is not visible from class loader.

How are people using ScriptRunner to use the new find interface?

At the moment I’m doing

ContentService contentService = ComponentLocator.getComponent(ContentService, 'apiContentService')
contentService.find()
   .withType(ContentType.PAGE)
   .withTitle('Test title')
   .fetchMany(new SimplePageRequest(0,50));

which blows up with something similar to

[runner.ScriptBindingsManager]: contentService: com.atlassian.confluence.api.impl.service.content.ContentServiceImpl@6e15dc74
[hibernate.ConfluenceHibernateTransactionManager]: Performing rollback. Transactions:
->[com.atlassian.confluence.api.impl.service.content.ContentServiceImpl.find]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly (Session #355171165)
[common.UserScriptEndpoint]: *****************************************************
[common.UserScriptEndpoint]: Script console script failed:
java.lang.IllegalArgumentException: interface com.atlassian.confluence.api.service.content.ContentService$ContentFinder is not visible from class loader
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:123)
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:113)
at org.springframework.aop.framework.ProxyFactotory.getProxy(ProxyFactory.java:97)
at com.atlassian.confluence.util.AopUtils.createAdvisedProxy(AopUtils.java:87)

at com.sun.proxy.$Proxy219.find(Unknown Source)

I’ve tried adding an Expansion (this worked for ContentPropertyService), but it doesn’t work for ContentService.

1 Like

@JeremyGoodwin - bit late responding, but I thought I’d ask, is all you want to do find a page based on its title? Because there are alternative ways you might do that.

You might just use a CQL query, for example.

import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.model.search.SearchContext
import com.atlassian.confluence.api.service.search.CQLSearchService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def cqlQuery = "title = 'Test title'"
def cqlSearchService = ScriptRunnerImpl.getOsgiService(CQLSearchService)
def searchResult = cqlSearchService.searchContent(
    cqlQuery,
    SearchContext.builder().build(),
    new SimplePageRequest(0, 50),
    Expansion.combine("space")
)

Paging through CQL results is a bit involved, but we have a documented example of how to do it you can peruse.

All that said, this does seem like a bug that ScriptRunner can’t access that class. I’ve logged it as such.
https://productsupport.adaptavist.com/browse/SRCONF-1963

You can watch/vote for that issue for updates if you really need the ContentService API for now.

Thanks for asking!