How to access team project list or Git project lis

2019-09-11 03:49发布

I am trying the following to get list of projects from "on prem" TFS

 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());
            }
        }

I am using a user name and password which has admin permissions on TFS i am trying to connect.But i get unauthorized access error when i try the above.

标签: tfs
2条回答
你好瞎i
2楼-- · 2019-09-11 04:12
  1. The REST API of getting a list of team projects is:

>

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
  1. Make sure you have enabled Basic Auth for your TFS:

    • check your IIS to see whether the Basic authentication service role is installed.
    • go to IIS Manager, select Team Foundation Server -- Authentication and disable everything other than Basic Authentication. Then do the same for the tfs node under Team Foundation Server.
    • restart your IIS.

enter image description here

enter image description here

查看更多
等我变得足够好
3楼-- · 2019-09-11 04:19

Here's a simple app using the Catalog Service. It looks for a file by cycling through all Project Collections and Projects, and finds instances of the file by name. It wouldn't take much to change it for your needs.

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);
        }
    }
}

}

查看更多
登录 后发表回答