Get spaces by keys with the Confluence API Part 2

Continued...

As promised from Get spaces by keys with the Confluence API article. below are the methods in a class that I created and examples of how to call them so that they're reusable.

I decided to create a separate article since the other one would have gotten huge and confusing.

Note

You can call your package whatever you want. I have a folder in the ScriptRunner Script Editor called util where I put reusable methods.

I also left out the notes for each line of code in this article.

The code

Main class "getSpace"

package util
    import com.atlassian.confluence.api.model.content.Space
    import com.atlassian.confluence.api.model.Expansion
    import com.atlassian.confluence.api.service.content.SpaceService
    import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
    import com.atlassian.confluence.api.model.pagination.SimplePageRequest
    import com.atlassian.confluence.api.model.pagination.PageResponse
    import org.apache.log4j.Logger
    import org.apache.log4j.Level

public class getSpace {

   public static Space getSpaceByKey(String spaceKey) {
       // find a single space by getting the space key

        SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
        
try {
Space space = spaceService.find(new Expansion('name'))
             .withKeys(spaceKey)
             .fetch()
             .get()

         return space
} catch (NoSuchElementException nse) {
            return null
        }

    }

   public static List<Space> getSpacesByKeys(spaceKeysint minResultsint maxResults) {
       // find multiple spaces by passing a String array of space keys

        SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
        PageResponse<Space> spaceResponse = spaceService.find(new Expansion('name'))
            .withKeys(spaceKeys)
            .fetchMany(new SimplePageRequest(minResults, maxResults))

        List<Space> spaces = spaceResponse.getResults() 
        return spaces
    }    

}

Call getSpaceByKey method

I included a Dynamic Form on this one to make it easy to lookup a spacekey, but really, you just need the line that starts with "Space" to get the space object.

 

import util.getSpace

import com.atlassian.confluence.api.model.content.Space
import com.onresolve.scriptrunner.parameters.annotation.*

@ShortTextInput(label = "Space Key", description = "Enter a single space key")
String spaceKey

Space space = getSpace.getSpaceByKey(spaceKey)

if (!space) {return "No spaces found."}

String result = "<p>${space.getKey()}${space.getName()}</p>"

Call getSpacesByKeys method

spaceKeys must be a String Array or it won't work.  Again, you really only need the line that starts with List<Space> (you don't have to specify the object type there), and you'll need to loop through the list to get the spaces.  The rest is for the example.

 

import util.getSpace

import com.atlassian.confluence.api.model.content.Space

String result = "" 
String [] spaceKeys = ['ABC','TEST']

List<Space> spaces = getSpace.getSpacesByKeys(spaceKeys,0,10)

if (!spaces) {return "No spaces found."

spaces.each { space ->
    result += "<p>${space.getKey()}${space.getName()}</p>"
}

return result

Minimal code

You'll still need the main class in full, but you can shrink the calling code down if you want.

Call getSpaceByKey method

import util.getSpace
def
space = getSpace.getSpaceByKey(spaceKey)

Call getSpacesByKeys method

import util.getSpace
def spaces = getSpace.getSpacesByKeys(['ABC','TEST'],0,10)
spaces.each { space ->
    space.getName() do whatever...
}

The end

There you go: a somewhat simple way of making what was in the first article reusable.

2 comments

Dominic Lagger
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.
February 13, 2022

Thanks for the code snippets!! 

Like WW likes this
Joni Graham July 1, 2024

Awesome, just what I needed.  Thanks!

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events