Get Queued Build using TFS API

In this post i will show how to get running build in all builds controllers / Agents.

TFS explorer shows the running build information for each TFS projects one by one, suppose you have many TFS project in your single collection and you want to track whats running now, you will send a lot of time to select all all build definitions and while you finish may other build has triggered.

1. Add the connect method to the form from the first post

2. Use this Code to get the information and view it data grid.

             DataTable _QueuedBuild = new DataTable();
            _QueuedBuild.Columns.Add("Status", typeof(String));
            _QueuedBuild.Columns.Add("definition", typeof(String));
            _QueuedBuild.Columns.Add("Priority", typeof(String));
            _QueuedBuild.Columns.Add("datequeued", typeof(String));
            _QueuedBuild.Columns.Add("requestedBy", typeof(String));
            _QueuedBuild.Columns.Add("Controller", typeof(String));
              //Az. bs is an object to access team build API
            IBuildServer bs = server.GetService();
            //Az. get all build spec, you can add any team project name or build definition or wildcards
            IQueuedBuildSpec qbSpec = bs.CreateBuildQueueSpec("*", "*");
            //Az. Get all gugued build now from specific team project collection
            IQueuedBuildQueryResult qbResults = bs.QueryQueuedBuilds(qbSpec);
             //Az. Filter the information the get the data you want to view it in grid
            foreach (IQueuedBuild qb in qbResults.QueuedBuilds)
                    string status = qb.Status.ToString();
                    string definition = qb.TeamProject + @"\" + qb.BuildDefinition.Name;
                    string Priority = qb.Priority.ToString();
                    string datequeued = qb.QueueTime.ToString();
                    string requestedBy = qb.RequestedBy;
                    string controller = qb.BuildController.Name;
                    if (qb.RequestedBy != qb.RequestedFor)
                        requestedBy = qb.RequestedBy + " (for " + qb.RequestedFor + ")";
                    _QueuedBuild.Rows.Add(status, definition, Priority, datequeued, requestedBy, controller);
            //Az. supose you will view the data in grid view
            dataGridView1.DataSource = _QueuedBuild;


Leave a Reply

Your email address will not be published. Required fields are marked *