2016-10-03 15 views
0

ich die folgenden bekommen Liste von Projekten aus zugreifen „auf Prem“ Ich versuche TFSWie Team Projektliste oder Git Projektliste mit TFS REST API

private static async void Method() 
     { 
      try 
      { 


       using (HttpClient 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(
           "http://test-test-app1:8080/tfs/boc_projects/_apis/projects?api-version=2").Result) 
        { 
         response.EnsureSuccessStatusCode(); 
         string responseBody = await response.Content.ReadAsStringAsync(); 
         Console.WriteLine(responseBody); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
     } 

Ich bin mit einem Benutzernamen und Passwort, das hat Admin-Berechtigungen für TFS Ich versuche zu verbinden. Aber ich bekomme unberechtigten Zugriff Fehler, wenn ich das oben versuchen.

+0

Ich würde empfehlen, den Katalog-Service, anstatt den REST-API verwenden, nur weil es nicht Admin-Zugriff erfordert. Ich habe Code dafür, wenn Sie interessiert sind. –

+0

Erzähl mir mehr über Katalog Service, ich bin mir dessen nicht bewusst –

Antwort

1
  1. Die REST-API von getting a list of team projects ist:

>

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0 
  1. sicher, dass Sie Basic-Auth für Ihren TFS aktiviert haben:

    • überprüfen Sie Ihre IIS, um zu sehen, ob der Standard-Authentifizierungsdienst Rolle ist installiert.
    • Wechseln Sie zum IIS-Manager, wählen Sie Team Foundation Server - Authentifizierung und deaktivieren Sie alles andere als die Standardauthentifizierung. Dann tun Sie die gleiche für den TFS-Knoten unter Team Foundation Server.
    • starten Sie Ihren IIS neu.

enter image description here

enter image description here

+0

Vielen Dank für Ihre Antwort, es funktioniert jetzt. –

0

Hier ist eine einfache Anwendung, die Katalog-Service. Es sucht nach einer Datei, indem es durch alle Projektkollektionen und -projekte wechselt, und findet Instanzen der Datei nach Namen. Es würde nicht viel brauchen, um es für Ihre Bedürfnisse zu ändern.

using System; 
using System.Linq; 
using Microsoft.TeamFoundation.Common; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Framework.Client; 
using Microsoft.TeamFoundation.Framework.Common; 
using Microsoft.TeamFoundation.VersionControl.Client; 

namespace EpsiFinder 
{ 
    internal class Program 
    { 
     // Server URL. Yes, it's hardcoded. 
     public static string Url = @"http://tfs.someserver.com:8080/tfs"; 

    private static void Main() 
    { 
     // Use this pattern search for the file that you want to find 
     var filePatterns = new[] { "somefile.cs" }; 

     var configurationServerUri = new Uri(Url); 
     var configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri); 
     var configurationServerNode = configurationServer.CatalogNode; 

     // Query the children of the configuration server node for all of the team project collection nodes 
     var tpcNodes = configurationServerNode.QueryChildren(
       new[] { CatalogResourceTypes.ProjectCollection }, 
       false, 
       CatalogQueryOptions.None); 

     // Changed to use the Catalog Service, which doesn't require admin access. Yay. 
     foreach (var tpcNode in tpcNodes) 
     { 
      Console.WriteLine("Collection: " + tpcNode.Resource.DisplayName + " - " + tpcNode.Resource.Description); 

      // Get the ServiceDefinition for the team project collection from the resource. 
      var tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"]; 
      var configLocationService = configurationServer.GetService<ILocationService>(); 
      var newUrl = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition)); 

      // Connect to the team project collection 
      var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(newUrl); 

      // This is where we can do stuff with the team project collection object 

      // Get the Version Control instance 
      var versionControl = tfs.GetService<VersionControlServer>(); 
      // Select the branches that match our criteria 
      var teamBranches = versionControl.QueryRootBranchObjects(RecursionType.Full) 
              .Where(s => !s.Properties.RootItem.IsDeleted) 
              .Select(s => s.Properties.RootItem.Item) 
              .ToList(); 
      // Match the file in the branches, spit out the ones that match 
      foreach (var item in from teamBranch in teamBranches 
           from filePattern in filePatterns 
           from item in 
            versionControl.GetItems(teamBranch + "/" + filePattern, RecursionType.Full) 
                .Items 
           select item) 
       Console.WriteLine(item.ServerItem); 
     } 
    } 
} 

}

Verwandte Themen