C# Octopus.Client - Retrieve all VariableSet(s) and data

Is there a way to get all of the variables using the Octopus.Client api?

We have a lot stored in Octopus and I want to compare the data in a spreadsheet to another document. I’m trying to write a simple utility to extract this info into a CSV.

Right now I have:

var server = “{server}”;
//var apiKey = “{key}”;
var endpoint = new OctopusServerEndpoint(server);
var repo = new OctopusRepository(endpoint);
repo.Users.SignIn(new LoginCommand() {Username = “{user}”, Password = “{password}”});

        List<LibraryVariableSetResource> library = repo.LibraryVariableSets.FindAll();
        
       //The signature of the VariableSets.Get() requires an array of string ids
       //I assume this to be the VariableSetId property on each LibraryVariableSetResource object returned from the call above
       
       //This keeps throwing a now found exception
        var variables = repo.VariableSets.Get(library.Select(x => x.VariableSetId).ToArray());
        
        var data = JsonConvert.SerializeObject(library);

Hi John

Thanks for reaching out!

Unfortunately not all resources in the API implement the overload of Get that take an array of id’s. Your best bet is to loop through the id’s and call the Get method for each one and add to a collection.

Hope that helps!

Regards,
Matt

Do I use the VaribleSetId on the VaribleSetResource object as the parameter?

Hi John

Yes, you should use the VariableSetId property:

    List<LibraryVariableSetResource> library = repo.LibraryVariableSets.FindAll();
    var variables = new List<VariableSetResource>();
    foreach(var variableSet in library)
        variables.Add(repo.VariableSets.Get(variableSet.VariableSetId));

Hope that helps.

Regards,
Matt