2

I have service hooks in my TFS Project. How is it possible to get info for service hooks by C#? I need the name of hook and value of State (Enabled or Disabled).

Is it possible?

State

derHugo
  • 83,094
  • 9
  • 75
  • 115
ignn
  • 29
  • 4

1 Answers1

1

You can use the REST API to get the value of state. Please see Get a list of subscriptions

PowerShell for example:

Param(
   [string]$baseUrl = "https://{account}.visualstudio.com/DefaultCollection/_apis/hooks/subscriptions",  
   [string]$user = "username",
   [string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))   
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$hooks = $response.value 

$states = @()
foreach($hook in $hooks){

    $customObject = new-object PSObject -property @{
          "consumer" = $hook.consumerId
          "id" = $hook.id
          "eventType" = $hook.eventType
          "state" = $hook.status
        } 
    $states += $customObject        
}   
$states | Select `
                consumer, 
                id, 
                eventType,
                state

To call a REST API using C#, you can reference this thread: How do I make calls to a REST api using c#?

You can also reference below sample:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetBuildsREST
{
    class Program
    {
        public static void Main()
        {
            Task t = GetBuildsREST();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task GetBuildsREST()
        {
            try
            {
                var username = "domain\\user";
                var password = "password";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "https://{account}.visualstudio.com/DefaultCollection/_apis/hooks/subscriptions?api-version=2.0").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                        //Do somthing to get the vale of the state....
                        //var obj = JObject.Parse(responseBody);
                        //var state = (string)obj["value"]["status"];
                        //or var state = (string)obj.SelectToken("value.status");

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55