Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

本文包含範例,說明如何使用舊版 SOAP 用戶端擴充和整合 Azure DevOps Server 和 Azure DevOps Services。 這些用戶端僅適用於用戶端的 .NET Framework 版本。

如需新的開發,請參閱 .NET 用戶端連結庫中 所述的 JSON 型用戶端。

此頁面上的範例需要下列 NuGet 套件:

Microsoft.TeamFoundationServer.ExtendedClient Microsoft.TeamFoundationServer.Client Microsoft.VisualStudio.Services.Client Microsoft.VisualStudio.Services.InteractiveClient

範例:使用SOAP型用戶端

// https://www.nuget.org/packages/Microsoft.TeamFoundationServer.ExtendedClient/
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
/// <summary>
/// This sample creates a new work item query under 'MyQueries', runs the query, and then sends the results to the console.
/// </summary>
public static void SampleSOAP()
    // create TfsTeamProjectCollection instance using default credentials
    using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri)))
        // get the WorkItemStore service
        WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
        // get the project context for the work item store
        Project workItemProject = workItemStore.Projects[teamProjectName];
        // search for the 'My Queries' folder
        QueryFolder myQueriesFolder = workItemProject.QueryHierarchy.FirstOrDefault(qh => qh is QueryFolder && qh.IsPersonal) as QueryFolder;
        if (myQueriesFolder != null)
            // search for the 'SOAP Sample' query
            string queryName = "SOAP Sample";
            QueryDefinition newBugsQuery = myQueriesFolder.FirstOrDefault(qi => qi is QueryDefinition && qi.Name.Equals(queryName)) as QueryDefinition;
            if (newBugsQuery == null)
                // if the 'SOAP Sample' query does not exist, create it.
                newBugsQuery = new QueryDefinition(queryName, "SELECT [System.Id],[System.WorkItemType],[System.Title],[System.AssignedTo],[System.State],[System.Tags] FROM WorkItems WHERE [System.WorkItemType] = 'Bug' AND [System.State] = 'New'");
                myQueriesFolder.Add(newBugsQuery);
                workItemProject.QueryHierarchy.Save();
            // run the 'SOAP Sample' query
            WorkItemCollection workItems = workItemStore.Query(newBugsQuery.QueryText);
            foreach (WorkItem workItem in workItems)
                // write work item to console
                Console.WriteLine("{0} {1}", workItem.Id, workItem.Fields["System.Title"].Value);

若要將驗證方法變更為 Azure DevOps,請在建立 VssConnection 時變更傳遞至 VssConnection 的 VssCredential 類型。

SOAP 服務的個人存取令牌驗證

public static void PersonalAccessTokenSoapSample()
    // Authenticate using Personal Access Token
    VssBasicCredential vssBasicCredential = new VssBasicCredential(string.Empty, pat);
    using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri), vssBasicCredential))
        tpc.Authenticate();
        Console.WriteLine(tpc.InstanceId);

Microsoft Entra 驗證用於 SOAP 服務

public static void AADSoapSample()
    // Authenticate using Azure Active Directory credential (requires a Azure AD-backed organization)
    using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri), new VssAadCredential()))
        tpc.Authenticate();
        Console.WriteLine(tpc.InstanceId);

適用於 SOAP 服務的 Visual Studio 登入提示 (Microsoft 帳戶或 Microsoft Entra 支援)

public static void MicrosoftAccountSample()
    // authenticate using Visual Studio sign-in prompt
    using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri), new VssClientCredentials()))
        tpc.Authenticate();
        Console.WriteLine(tpc.InstanceId);