Azure DevOps Server - 获取项目的“范围”

Azure DevOps Server - get `scope` of the project

我有 Azure DevOps Server v2019.Update1.1,我想使用 tfssecurity cli 在项目中创建新组(因为图形 api 尚不可用于内部部署)

为此,我需要 scope 参数来标识项目。来自文档:"Specifies the URI of the project for which you want to display groups. To obtain the URI for a project, open Team Explorer, right-click the project, click Properties, and copy the entire entry for URL"

google 结果看起来像 "vstfs:///Classification/TeamProject/6868ab48-73f8-499d-b4c5-bb743f68ad87"

有没有办法通过 cli 或 api 以编程方式获取该值?

Scope是项目的URI,格式为vstfs://Classification/TeamProject/00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000 是项目的id。您可以从 Projects - List REST api:

获取项目 ID

GET https://{instance}/{collection}/_apis/projects?api-version=5.0

或者您可以使用以下代码检索 URI:

using System;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;

namespace GetTeamProject
{
    class Program
    {
        static void Main(string[] args)
        {

            VssCredentials cred = new VssClientCredentials();
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://TFS2019:8080/tfs/DefaultCollection"), cred);
            tpc.EnsureAuthenticated();
            ICommonStructureService css = tpc.GetService<ICommonStructureService>();
            ProjectInfo[] projects = css.ListProjects();
            foreach (ProjectInfo pro in projects)
            {
                Console.WriteLine(pro.Name);
                Console.WriteLine(pro.Uri);
            }

        }
    }
}