How to use ContentService instead of PageManager?
Asked by ABoerio:

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


Answers:
author: Tiffany Wortham
body: 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}")
```

comments: author: ABoerio
body: Thank you very much.

Andrea.


author: Jeremy Goodwin
body: Great find, thanks [@Tiffany Wortham](/t5/user/viewprofilepage/user-id/1420833) , 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
...
```


author: Tiffany Wortham
body: 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

comments: author: ABoerio
body: Hi [@Tiffany Wortham](/t5/user/viewprofilepage/user-id/1420833) thanks for your hint. I'll for sure give it a try.

Just need to find a moment.

Txs, Andrea


author: ABoerio
body: 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


author: Tiffany Wortham
body: 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.


author: ofendri
body: 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.


author: ABoerio
body: 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


author: ofendri
body: That is true, but I'm using IntelliJ for efficiency.


author: ABoerio
body: 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.


Tags: api

confluence-server

scriptrunner

server