How to use ContentService instead of PageManager?

ABoerio December 1, 2020

Hi, 

in my SR scripts I often use PageManager methods (especially getPage()) to get information about pages.

As by https://docs.atlassian.com/ConfluenceServer/javadoc/7.9.1/  I noticed that getPage() is deprecated, and ContentService.find() is recommended.

Sorry to admit that I've never used yet succesfully ContentService to get done what I need, due to my knowledge lack.

Looking at the following simple code, which obviously runs with no problems in my instance:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page

def myKnownPageTitle='Title of my page'
def mySpaceString='Name of my space'

def pageManager = ComponentLocator.getComponent(PageManager)
def myPage = pageManager.getPage(mySpaceString, myKnownPageTitle)
def myRetrievedId = myPage.getId()
def myRetrievedPageVersion=myPage.getVersion()

log.warn("Page searched: ${myKnownPageTitle}")
log.warn("Retrieved page ID is: ${myRetrievedId}")
log.warn("Current page version is: ${myRetrievedPageVersion}")

How should I implement ContentService to get the same result?

I have no problem in living with deprecated methods (until they run :-)) but of course I'd like to progressively improve.

Thanks in advance for any hint.

Ciao, Andrea

 

 

2 answers

1 accepted

1 vote
Answer accepted
Tiffany Wortham
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.
April 27, 2021

Here's the updated script from the Champion Hour!

import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.content.ContentType
import com.atlassian.confluence.api.service.content.ContentService
import com.atlassian.confluence.api.service.content.SpaceService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def contentService = ScriptRunnerImpl.getPluginComponent(ContentService)
def spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)

def title ='Welcome To Confluence'

def space = spaceService.find(new Expansion('name'))
.withKeys('ds')
.fetch()
.get()

def findMatchingPage = contentService.find(new Expansion('version'), new Expansion('id'))
.withType(ContentType.PAGE)
.withSpace(space)
.withTitle(title)
.fetch()

def page = findMatchingPage.get()
def pageId = page.id
def pageVersion = page.version

log.debug("Page searched: ${title}")
log.debug("Retrieved page ID is: ${pageId}")
log.debug("Current page version is: ${pageVersion}")
ABoerio May 3, 2021

Thank you very much.

Andrea.

Jeremy Goodwin June 10, 2021

Great find, thanks @Tiffany Wortham , this finally answers how to use these new api interfaces which fall over when using the ComponentLocator approach.

 

Just as a note, I've also since found that you don't need to use ScriptRunnerImp.getPluginComponent, instead you can use the @PluginModule annotation and have it injected directly (I assume the annotation uses getPluginComponent in the background..)

 

e.g.

import com.onresolve.scriptrunner.runner.customisers.PluginModule;

...

@PluginModule
SpaceService spaceService
...
Like Tiffany Wortham likes this
0 votes
Tiffany Wortham
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.
April 15, 2021

Hi Andrea! Here's what it would look like if you changed this script to use ContentService.find()

import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.content.ContentType
import com.atlassian.confluence.api.service.content.ContentService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def contentService = ScriptRunnerImpl.getPluginComponent(ContentService)

def myKnownPageTitle='Welcome To Confluence'

def findMatchingPages = contentService.find(new Expansion('version'), new Expansion('id'))
.withType(ContentType.PAGE)
.withTitle(myKnownPageTitle)
.fetch()

def page = findMatchingPages.get()
def pageId = page.id
def pageVersion = page.version

log.warn("Page searched: ${myKnownPageTitle}")
log.warn("Retrieved page ID is: ${pageId}")
log.warn("Current page version is: ${pageVersion}")

I Hope this helps! Let me know (:
Tiffany

ABoerio April 19, 2021

Hi @Tiffany Wortham thanks for your hint. I'll for sure give it a try.

Just need to find a moment.

Txs, Andrea

ABoerio April 19, 2021

Hi again, 

I guess I should include in your code an additional

.with... 

to indicate the Space in want to search in, right?

Ciao, Andrea

Tiffany Wortham
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.
April 20, 2021

Yes, that would probably be best since it's possible that two different pages in two different spaces could have the same name (I think), and I didn't think of that. 

ofendri May 11, 2021

Hello, which dependencies do you use to import com.onresolve.scriptrunner.runner.ScriptRunnerImpl please?

I don't seem to have that library and I need to build it from my gradle first.

ABoerio May 11, 2021

Hi,

When running script from the Scriptrunner console all the needed valid resources from Adaptivist  should be already available for import, isn't it.

Ciao, Andrea

ofendri May 11, 2021

That is true, but I'm using IntelliJ for efficiency.

ABoerio May 11, 2021

OK, then I'm sorry, I can't help, I'm writing and managing my scripts into the script editor, and then calling them from the console (or from jobs, endpoints, etc ..).

Ciao, Andrea.

Suggest an answer

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

Atlassian Community Events