Sasi Kanth Sasi Kanth - 1 year ago 102
PowerShell Question

Total number of labels inside a module of TFS repository

I need to find out how many labels are there inside each module of a collection in Team Foundation repository.
I am using TFS 2013.
I know we can get it from Visual Studio. But we need a script which gets us the number of labels as output.
Can anyone help me out in getting a C# or a Powershell code to obtain the same?


Answer Source

You can use .NET Client Libraries to get this: .NET client libraries for Visual Studio Team Services (and TFS)

Code sample:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace GetLabels
    class Program
        static void Main(string[] args)
            string tfscollection = "http://xxx:8080/tfs/defaultcollection";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfscollection));
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            string labelname = null;
            string labelscope = "$/";
            string owner = null;
            bool includeitem = false;
            int labelnumber;
            VersionControlLabel[] labels = vcs.QueryLabels(labelname,labelscope,owner,includeitem);
            labelnumber = labels.Length;