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,458,706
Community Members
 
Community Events
176
Community Groups

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.

1 comment

Dominic Lagger Community Leader Feb 13, 2022

Thanks for the code snippets!! 

Like WW likes this

Comment

Log in or Sign up to comment
TAGS

Atlassian Community Events