Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to use ContentService instead of PageManager?

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

0 votes
Answer accepted

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}")

Thank you very much.

Andrea.

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

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

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

Just need to find a moment.

Txs, Andrea

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

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. 

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.

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

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

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
Community showcase
Published in Confluence

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,386 views 7 29
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you