简体   繁体   English

如何从 WPF 或 Windows 应用程序获取 azure DevOps 服务器中所有版本的项目列表

[英]How to get list of all projects with versions in the azure DevOps server from WPF or Windows application

I want to list and display all project files in Azure DeveOps server from a windows/WPF application.我想从 Windows/WPF 应用程序中列出并显示 Azure DeveOps 服务器中的所有项目文件。 Ealier My application retrieve data from TFS server.更早我的应用程序从 TFS 服务器检索数据。 Now our repository is changed and Now Azure devops server is used instead of TFS. I am looking for equivalent of below code which is used in TFS. I want to do same operation with Azure DevOps Server.现在我们的存储库已更改,现在使用 Azure devops 服务器而不是 TFS。我正在寻找与 TFS 中使用的以下代码等效的代码。我想对 Azure DevOps 服务器执行相同的操作。

Sample Code to get all project files and objects from TFS从 TFS 获取所有项目文件和对象的示例代码

 Uri tfsUri;            
        string OutPutFolderDB, DirectoryNextTo, Path;
        tfsUri = new Uri("http://" + serverURI + "/testProjectCollection");
        var targettfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
        Microsoft.TeamFoundation.Server.ICommonStructureService stuctureService =
       targettfs.GetService<Microsoft.TeamFoundation.Server.ICommonStructureService>();

        Microsoft.TeamFoundation.Server.ProjectInfo[] projects = stuctureService.ListAllProjects();
        VersionControlServer vcs = targettfs.GetService<VersionControlServer>();

I would like to know similar kind of above code which is used in Azure devops server.我想知道在 Azure devops 服务器中使用的类似上述代码。 My repository is git.我的存储库是 git。

We recommend that you can use REST API to list all projects in the power shell script.我们建议您可以使用 REST API 列出电源 shell 脚本中的所有项目

Sample:样本:

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$OrgUrl = "https://dev.azure.com/{Org name}/_apis/projects?api-version=6.0-preview.4" 
$ListProject = (Invoke-RestMethod -Uri $OrgUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$ListProjectCount = $ListProject.count
Write-Host  $ListProjectCount 

$ListProjectName= $ListProject.value.name

ForEach ($Name in $ListProjectName)
{
Write-Host  $Name
}

Result:结果: 在此处输入图像描述

We also can use client API to list the project, corresponding Code in C#:我们也可以使用客户端API来列出项目,对应的代码在C#中:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("{org url}");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", {credentials});

    HttpResponseMessage response = client.GetAsync("_apis/projects?$top=250&stateFilter=All&api-version=1.0").Result;


    if (response.IsSuccessStatusCode)
    {
        responseString = response.Content.ReadAsStringAsync().Result.ToString();
    }
    else
    {
        //failed
    }
}

// Convert responseString into a json Object
RootObj jsonObj = JsonConvert.DeserializeObject<RootObj>(responseString);
Console.WriteLine("Found " + jsonObj.Count + " projects");

//Do stuff
foreach (var obj in jsonObj.Value)
{
    //foreach project...
}

Update1更新1

List all repositories: 列出所有存储库:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.0-preview.1

We can get the repo ID, then You can use the API below to list repo files:我们可以得到 repo ID,然后你可以使用下面的 API 列出 repo 文件:

GET https://dev.azure.com/{org name}/{project name}/_apis/git/repositories/{repo ID}/items?recursionLevel=Full&api-version=4.1

Result:结果:

在此处输入图像描述

If you want download all these:如果你想下载所有这些:

click here 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM