Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Tiffany Wortham thanks for your hint. I'll for sure give it a try.
Just need to find a moment.
Txs, Andrea
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.