首页 > 解决方案 > 如何使用 TFS 2015 上的 devops api 检索所有“项目集合”的列表

问题描述

我正在尝试使用 c# 通过 devops API 检索 TFS 2015 服务器上所有项目集合的列表。我没有直接访问 TFS 配置数据库的权限。

我发现的更多解决方案使用旧的或不推荐使用的方法,如tfsConfigurationServerFactoryclass.

标签: c#tfsazure-devopstfs-2015azure-devops-rest-api

解决方案


您是否尝试过项目集合 API

样品要求:

GET https://mytfsserver/DefaultCollection/_apis/projectCollections/d81542e4-cdfa-4333-b082-1ae2d6c3ad16?api-version=1.0-preview.2

示例响应:

{
    "id": "d81542e4-cdfa-4333-b082-1ae2d6c3ad16",
    "name": "DefaultCollection",
    "url": "https://mytfsserver/DefaultCollection/_apis/projectCollections/d81542e4-cdfa-4333-b082-1ae2d6c3ad16",
    "state": "Started",
    "_links": {
        "self": {
            "href": "https://mytfsserver/DefaultCollection/_apis/projectCollections/d81542e4-cdfa-4333-b082-1ae2d6c3ad16"
        },
        "web": {
            "href": "https://mytfsserver/DefaultCollection"
        }
    }
}

还有一些示例 C# 代码的链接。

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace Microsoft.TeamServices.Samples.Client.ProjectsAndTeams
{
    [ClientSample(CoreConstants.AreaName, CoreConstants.ProjectCollectionsResource)]
    public class ProjectCollectionsSample : ClientSample
    {

        [ClientSampleMethod]
        public IEnumerable<TeamProjectCollectionReference> ListProjectCollections()
        {         
            VssConnection connection = Context.Connection;
            ProjectCollectionHttpClient projectCollectionClient = connection.GetClient<ProjectCollectionHttpClient>();

            IEnumerable<TeamProjectCollectionReference> projectCollections = projectCollectionClient.GetProjectCollections().Result;

            foreach(var collection in projectCollections)
            {
                Console.WriteLine(collection.Name);
            }

            return projectCollections;
        }

    }
}

推荐阅读