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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,054
Community Members
 
Community Events
176
Community Groups

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

1 vote
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

Atlassian Community Events