How to get list of Spaces

Anyone have a code sample to get a list of Spaces using Octopus Client? I have searched all over and tried many things to no avail. Thanks in Advance.

Hi @rfreeland,

Thanks for reaching out.

This code should do the trick:

Add-Type -Path 'C:\Temp\Octopus.Client.dll'  #location of Octopus.Client.dll on the machine running the script

# Octopus variables
$Apikey = "API-"
$OctopusURI = "https://"

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURi, $Apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$client = New-Object Octopus.Client.OctopusClient $endpoint

$spaces = $repository.Spaces.GetAll()
 $spaces

Please let me know if that works on your end.

1 Like

Im using c# but I have basically the same thing you have and iSpaceRepository does not seem to contain a definition for ‘GetAll’

Here is my code:

        string strAPIKey = ConfigurationManager.AppSettings.Get("OctopusApiKey");
        string strServer = ConfigurationManager.AppSettings.Get("OctopusURL");

        var endpoint = new Octopus.Client.OctopusServerEndpoint(strServer, strAPIKey);
        var client = new Octopus.Client.OctopusClient(endpoint);
        var repository = new OctopusRepository(endpoint);
        var spaces = repository.Spaces.GetAll(); (This does not seem to work)
1 Like

Hey @rfreeland,

Can you please try this code?

var spaces = repository.Spaces.FindAll();
foreach (var space in spaces){
Console.WriteLine(space.Name);
}

@jeremy.miller, I already tried this and it only seems to return 1 object which is the Default space. Must be missing something on this. I appreciate your help on this.

This is my code:

#r "C:\Temp\Octopus.Client.dll"

using Octopus.Client;
using Octopus.Client.Model;

var octopusURL = "https://";
var octopusAPIKey = "API-";

var endpoint = new OctopusServerEndpoint(octopusURL, octopusAPIKey);
var repository = new OctopusRepository(endpoint);

var spaces = repository.Spaces.FindAll();
foreach (var space in spaces){
Console.WriteLine(space.Name);
}

Output:

image

I am using 11.3.3425 version of Octopus.Client.dll

Can you try using my exact script and see what your output is?

Best,
Jeremy

@jeremy.miller This is using your exact code and it only shows 1 space.
image

I have 3 active and valid spaces in Octopus

@rfreeland

Can you try creating a new API Key that you know has full permissions to all the spaces and use that one instead?

I’m wondering if that API key can only see the one space and that’s what may be causing this.

Looking forward to hearing back.

Best,
Jeremy

@jeremy.miller, That was it. Thank You for your help. Apparently the API key was generated back before we had other spaces. The only drawback I see to this is that every time we create a new Space we will have to generate another API key? Does that seem to make sense ? This means we will have to redeploy this piece of our automation every time we create a new Space.

Hi @rfreeland,

You’re very welcome.

It shouldn’t be necessary as when you create a new space it will ask which users/team will be a space manager. As long as the API key you’re using belongs to one of the selected options, the API key should have privileges to that space.

image

Can you try creating a dummy space and testing the script again, then deleting the dummy space?

Please let me know.

Best,
Jeremy

@jeremy.miller, I added and deleted a Space and it seems to work with the new API key. Thank You so much for your expedient help on this. Very much appreciated.

1 Like

@rfreeland,

You’re very welcome! Thanks for letting me know.

I hope you have a great rest of your week.

Best,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.