Share via


Using ReleaseHttpClient for interacting with ReleaseManagement service

ReleaseHttpClient is public as nuget package : https://www.nuget.org/packages/Microsoft.VisualStudio.Services.Release.Client

GitHub samples: https://github.com/chandan-anjani/VSTSRMAPISample

Fetching release definitions using given credential :-

Step1: Create one 'Windows Console Application' using Visual studio 2015

Step2: Goto Tools -->NuGet Package Manager --> Manage NuGet Packages for Solutions --> Browse (make sure Package source: nuget.org)

Step3: Search and Install following package to your solution

  •  Microsoft.VisualStudio.Services.Release.Client
  • Microsoft.VisualStudio.Services.InteractiveClient

Step4: Add following code to fetch all release definitions for a given project. Please update your tenant and project name in below sample.

 using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;

namespace ReleaseHttpClientSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri serverUrl = new Uri("https://{your tenant name}.visualstudio.com");
            VssCredentials credentials = new VssClientCredentials();
            credentials.Storage = new VssClientCredentialStorage();

            VssConnection connection = new VssConnection(serverUrl, credentials);

            ReleaseHttpClient rmClient = connection.GetClient<ReleaseHttpClient>();

             var releaseDefinitions = rmClient.GetReleaseDefinitionsAsync("{your project name}").Result;

            Console.Out.WriteLine("Release definitions " + releaseDefinitions.Count);
        }
    }
}

Step5: Done

 

 Adding a variable to a given release using PAT Token :-

Follow Step1 to Step3 above

Step4:-

 using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;

namespace ReleaseHttpClientSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string collectionUri = args[0];
            string projectName = args[1];
            string patToken = args[2];
            int releaseId = int.Parse(args[3]);

            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssBasicCredential("username", patToken));
            ReleaseHttpClient client = connection.GetClient<ReleaseHttpClient>();
            var release = client.GetReleaseAsync(projectName, releaseId).Result;

            var variableValue = new ConfigurationVariableValue();
            variableValue.Value = "bar";
            release.Variables.Add("foo", variableValue);

            var updatedRelease = client.UpdateReleaseAsync(release, projectName, releaseId).Result;

     } 
   } 
}

Step5: Done