Banoona
Banoona

Reputation: 1650

How to use the continuationtoken in TFS 2015 Object Model: GetBuildsAsync?

I am using the following code

    BuildHttpClient service = new BuildHttpClient(tfsCollectionUri, 
new Microsoft.VisualStudio.Services.Common.VssCredentials(true));

    var asyncResult = service.GetBuildsAsync(project: tfsTeamProject);
    var queryResult = asyncResult.Result;

This returns only the first 199 builds.

Looks like in need to use the continuationtoken but am not sure how to do this. The docs say that the REST API will return the token. I am using the Object Model, and am looking for how to retrieve the token!

I am using Microsoft.TeamFoundationServer.Client v 14.102.0; Microsoft.TeamFoundationServer.ExtendedClient v 14.102.0, Microsoft.VisualStudio.Service.Client v 14.102.0 and Microsoft.VisualStudio.Services.InteractiveClient v 14.102.0

Question How do I use the continuation token **when using the TFS Object model?

Upvotes: 0

Views: 753

Answers (2)

PainElemental
PainElemental

Reputation: 757

You have to use 'GetBuildsAsync2', which returns an IPagedList. You can retrieve the ContinuationToken from the IPagedList:

        // Iterate to get the full set of builds
        string continuationToken = null;
        List<Build> builds = new List<Build>();
        do
        {
            IPagedList<Build> buildsPage = service.GetBuildsAsync2(tfsTeamProject, continuationToken: continuationToken).Result;

            //add the builds
            builds.AddRange(buildsPage);

            //get the continuationToken for the next loop
            continuationToken = buildsPage.ContinuationToken;
        }
        while (continuationToken != null);

Upvotes: 1

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31023

The continuationToken is in the response header after the first call to the API:

x-ms-continuationtoken: xxxx

It can not be retrieved from .net client library. You have to use the rest api to retrieve the header information. Here is an example for your reference:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetBuilds
{
    class Program
    {
        public static void Main()
        {
            Task t = GetBuilds();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task GetBuilds()
        {
            try
            {
                var username = "xxxxx";
                var password = "******";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "http://tfs2015:8080/tfs/DefaultCollection/teamproject/_apis/build/builds?api-version=2.2").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Upvotes: 1

Related Questions