C# client library samples for SOAP clients and services

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

This article contains samples that show you how to extend and integrate with Azure DevOps Server and Azure DevOps Services using the legacy SOAP clients. These clients are only available in the .NET Framework version of the clients.

Important

For new development, see the JSON-based clients described in .NET client libraries.

Prerequisites

Examples on this page require the following NuGet packages:

Example: Using SOAP-based client

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

Authentication

To change the method of authentication to Azure DevOps, change the VssCredential type passed to VssConnection when you create it.

Personal access token authentication for SOAP services

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 authentication for SOAP services

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

Visual Studio sign-in prompt (Microsoft Account or Microsoft Entra backed) for SOAP services

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