如何使用TFS REST API访问团队项目列表或Git项目列表

时间:2016-10-03 10:31:30

标签: tfs

我正在尝试以下方法从“oncement”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());
            }
        }

我使用的用户名和密码对TFS具有管理员权限我正在尝试连接。但是当我尝试上述操作时,我收到了未经授权的访问错误。

2 个答案:

答案 0 :(得分:1)

  1. getting a list of team projects的REST API是:
  2. >

    http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
    
    1. 确保为TFS启用了基本身份验证:

      • 检查您的IIS以查看是否已安装基本身份验证服务角色。
      • 转到IIS管理器,选择Team Foundation Server - 身份验证 并禁用基本身份验证以外的所有内容然后做 对于Team Foundation Server下的tfs节点也是如此。
      • 重新启动IIS。
    2. enter image description here

      enter image description here

答案 1 :(得分:0)

这是一个使用目录服务的简单应用程序。它通过遍历所有项目集合和项目来查找文件,并按名称查找文件的实例。根据您的需要改变它不会花费太多。

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

}