Tarun Arora

Visual Studio ALM MVP
posts - 58, comments - 187, trackbacks - 0

My Links

News


Tarun Arora is a Microsoft Certified professional developer for Enterprise Applications. He has extensively travelled around the world gaining experience learning and working in culturally diverse teams. Tarun has over 5 years of experience developing 'Energy Trading & Risk Management' solutions for leading Trading & Banking Enterprises. His passion for technology has earned him the Microsoft Community Contributor and Microsoft MVP Award.




View Tarun Arora's profile on LinkedIn

profile for Tarun Arora at Stack Overflow, Q&A for professional and enthusiast programmers

Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

TFS SDK: Workspace Explorer & Statistics

 

The Team Explorer is a wonderful view of the position of your version control in the team foundation server. However, there is no one window of such truth available for the workspace. I am a very active member of the msdn community and have come across questions on,

  • How do I see a list of all files that are not mapped to TFS but are in my workspace? (Yes, you can use the TFS power tools tfpt.exe /scorch or /treeclean to achieve this)
  • I would like to see all pending changes in just one folder and not recursively in the subfolders.
  • I would like to analyse my workspace, what file types do i have, what is the size of file by type, etc.

There are workarounds available, but because TFS does not provide these features natively, the experience is nothing as compared to browsing sever artificats through Team Explorer.

image

Welcome to Workspace Explorer – A solution using TFS API

1. Get a list of all workspaces, all workspace and multiple folder mappings under a workspace. I have seen developers map multiple folders under a workspace and then complain it takes too long to do get latest, not all go to the workspace folder mapping to verify for what are they requesting the get latest?

2. Explorer Style Navigation, along with a column to indicate the pending change type and a column to indicate whether the file in workspace is mapped to version control. When peer reviews are carried using shelve sets reviewers have new files from the shelve sets left in their workspace.

3. Workspace Statistics – A chart to break down the files by extension and size, this can be done both at the current directory level or recursively for all files in directory and subdirectories.

4. Pending Changes – A list of all pending changes, this again can be done both at the current directory level or recursively.

5. Missing Mappings – A list of all files in the workspace not mapped to the server, this again can be done at the current directory level or recursively.

 

1. Get all workspaces programmatically using TFS API

I have a blog post on workspaces – Get List of user workspace and checked out files programmatically

Add a new ListView and set the property view to ‘Details’. Add the columns you would like to display to the property ‘Columns’.

private void PopulateWorkspaceTracker()
        {
            // Connect to TFS - VersioControlServer service
            var tfs =
                TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://tfs2010:8080/defaultcollection"));
            var vcs = tfs.GetService<VersionControlServer>();

            // Get all workspaces for the user on the machine
            var workspaces = vcs.QueryWorkspaces(null, vcs.AuthorizedUser, Environment.MachineName);

            // Clear the ListView control
            lstWorkspaceDetails.Items.Clear();
            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;

            // For all workspaces add the values to the columns
            foreach (var workspace in workspaces)
            {
                foreach (var wk in workspace.Folders)
                {
                    item = new ListViewItem(workspace.Name, 0);
                    subItems = new[]
                               {
                                   new ListViewItem.ListViewSubItem(item, wk.LocalItem),
                                   new ListViewItem.ListViewSubItem(item, wk.ServerItem), 
                                   new ListViewItem.ListViewSubItem(item, workspace.EffectivePermissions.ToString()),
                                   new ListViewItem.ListViewSubItem(item, wk.IsCloaked.ToString()),
                                   new ListViewItem.ListViewSubItem(item, workspace.OwnerName)
                               };
                    item.SubItems.AddRange(subItems);
                    lstWorkspaceDetails.Items.Add(item);
                }
            }
            lstWorkspaceDetails.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }

 

2. Explorer like navigation for the workspace folders

Well, i have created an explorer style interface using ListView and TreeView controls. Click here for a walkthrough on how to do this.

You can use the code below if do not want to follow the walkthrough at this time.

private void PopulateTreeView(string workspaceName)
{
    // Connect to TFS - VersionControlServer Service
    var tfs =
        TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://tfs2010:8080/defaultcollection"));
    var vcs = tfs.GetService<VersionControlServer>();

    // Get the workspace the user has currently selected
    var workspace = vcs.QueryWorkspaces(workspaceName, vcs.AuthorizedUser, Environment.MachineName)[0];
    _workspace = workspace;
    tvWksNavigator.Nodes.Clear();

    // Loop through all folders and get directories and files
    foreach (var folder in workspace.Folders)
    {
        var info = new DirectoryInfo(folder.LocalItem);
        if (info.Exists)
        {
            var rootNode = new TreeNode(info.Name) { Tag = info };
            GetDirectories(info.GetDirectories(), rootNode);
            tvWksNavigator.Nodes.Add(rootNode);
        }
    }
}

// Get directories recursively and add them to the tree view
private static void GetDirectories(IEnumerable<DirectoryInfo> subDirs,
                            TreeNode nodeToAddTo)
{
    TreeNode aNode;
    DirectoryInfo[] subSubDirs;
    foreach (var subDir in subDirs)
    {
        aNode = new TreeNode(subDir.Name, 0, 0);
        aNode.Tag = subDir;
        aNode.ImageKey = "folder";
        subSubDirs = subDir.GetDirectories();

        if (subSubDirs.Length != 0)
        {
            GetDirectories(subSubDirs, aNode);
        }
        nodeToAddTo.Nodes.Add(aNode);
    }
}

 

3. Check if a file is pending change programmatically using TFS API

Using the method QueryPendingChanges, it is possible to pass a file path and see if the file is pending change, if so, what is lock type, and also get the download details for the file.

var status = _workspace.QueryPendingSets(new[] { new ItemSpec(
                                                                dir.FullName, 
                                                                RecursionType.None) },
                                                                _workspace.Name, vcs.AuthorizedUser, false);

 

4. Check if a file in workspace is mapped to Version Control using TFS API

Using the method ServerItemExists, it is possible to pass a file path and see if the file has a server mapping.

var isValid =
    _workspace.VersionControlServer.ServerItemExists(
        _workspace.GetServerItemForLocalItem(dir.FullName), ItemType.Any);

 

5. Workspace Statistics

The chart control impressed me, with little customizations i could easily generate visually attractive graphs to visualise my workspace for files by extension and size. Add a chart control and use the below code. You can also look at the possibilities with chart controls on this interesting msdn read here => http://archive.msdn.microsoft.com/mschart

// Check whether the user has requested all files or files
// in just the selected folder. 
if (cbRecursionType.SelectedItem == "Recursive")
{
    // Use a simple stack to get all directories and files
    while (stack.Count > 0)
    {
        var dir = stack.Pop();

        filesInformation.AddRange(dir.GetFiles("*.*"));

        foreach (var dn in dir.GetDirectories("*.*"))
        {
            stack.Push(dn);
        }
    }    
}
else // current directory
{
    while (stack.Count > 0)
    {
        var dir = stack.Pop();
        filesInformation.AddRange(dir.GetFiles("*.*"));
    }
}

// Get all distinct extensions from all files
var extensions = filesInformation.Select(f => f.Extension).Distinct();
// Group files by extensions
var workspaceStatisticsCollection = extensions.Select(extension => (from fileInfo in filesInformation
                                                                    where fileInfo.Extension == extension
                                                                    select new WorkspaceStatistic() { File = fileInfo }).ToList()).ToList();

// Statistics - chart
var yval = new int[extensions.ToList().Count];
var xval = new string[extensions.ToList().Count];
var count = 0;

// Add points to x and y axis 
foreach (var workspaceStats in workspaceStatisticsCollection)
{
    var extension = extensions.ToArray()[count];
    // Y = File count
    yval[count] = workspaceStats.Count;
    // X = Extensions
    xval[count] = extension;
    count += 1;
}

// Some designing to the chart
Series series1 = chart1.Series[0];
chart1.Series[0].ChartType = SeriesChartType.Bar;
chart1.Series[0]["PointWidth"] = "0.6";
chart1.Series[0].IsValueShownAsLabel = true;
chart1.Series[0]["BarLabelStyle"] = "Center";
chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
chart1.Series[0]["DrawingStyle"] = "Emboss";
chart1.Series[0].Points.DataBindXY(xval, yval);
chart1.Series[0].Name = "FilesByExtension";
chart1.Visible = true;
chart1.DataBind();
lblTotal.Text = String.Format("Total {0} files, compute to {1} bytes", filesInformation.Count,
                              filesInformation.Sum(f => f.Length));

 

And done! I have a working solution, but i am yet to test various scenarios. I will upload the code to codeplex once tested (yes, i am a responsible developer). Any ways, if you find this interesting and want a copy of the working solution, please feel free to email me and i’ll be happy to share the code with you.

What do you think, good bad ugly, share your feedback?

 

Share this post :

Print | posted on Tuesday, August 30, 2011 7:54 PM | Filed Under [ TFS2010 TFS API TFS SDK ]

Feedback

Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Good article. We are also missing some statistical functions like the one shown in you article. I would really be greatefull if you could send me a working copy of your current solution, regardless how well tested it is ;)

Keep up the good work, and I'll be coming back to this blog more often!!

Regards, Emil
9/6/2011 8:21 AM | Emil Cristen
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi Emil,

Thanks for your kind comment. I have emailed you the solution. If you have questions or feedback please feel free to add to the thread.

Best wishes,
Tarun
9/7/2011 1:28 AM | Tarun Arora
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Great article !!!. How can I downlod Workspace Explorer, please you could send me a working copy of your current solution, or url in coeplex ?

I think you're guru abou TFS,great blog, I'll be coming back to this blog more often!!

Regards,greeting
9/20/2011 10:45 AM | kiquenet
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi Kiquenet,

Thanks for your kind comment. I have email you the solution. If you have questions or feedback please feel free to add to the thread. Sorry i haven't got around posting the actual solution to codeplex, i have been distracted by the preview of TFS 2011 :-)

Cheers, Tarun
9/20/2011 10:59 AM | Tarun Arora
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Very good work. Can you please send me working copy of your current solution?

Thanks in advance.
12/1/2011 7:12 AM | Hitesh
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi Hitesh,

Thanks for your kind comment. I have email you the solution. If you have questions or feedback please feel free to add to the thread.

Cheers, Tarun
12/1/2011 9:49 AM | Tarun Arora
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Very interesting and useful! I'd be very much obliged if you could send me the source code.
12/16/2011 6:41 PM | H. Noell
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi H.Noell,

Thanks for your kind comment. I have email you the solution. If you have questions or feedback please feel free to add to the thread.

Cheers, Tarun
12/17/2011 10:05 AM | Tarun Arora
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Very good articles. You helped me a lots with your articles. This one is very interesting. Is the source now on Codeplex?
Could I have a copy of the solution.

Thanks in advance.
1/4/2012 5:32 PM | S. Lavoie
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi,

Thanks for your kind comment. I have email you the solution. If you have questions or feedback please feel free to add to the thread.

Cheers, Tarun
1/4/2012 6:47 PM | Tarun Arora
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Thanks for the article.
Can I see the full source code? (it would be very useful)

WBR,
Kirill Temnenkov
4/12/2012 8:53 AM | Kirill Temnenkov
Gravatar

# re: TFS SDK: Workspace Explorer & Statistics

Hi Tarun!

As usually; yet another great post (You cannot realize how often you already helped me with your blog !)

I would now be very interested in your code. The whole solution :) But especially the part computing the size of the sources. It would help me to figure out how to implement a batch monitoring the growth of the data. I am indeed afraid that end-users could store unexpected items like vhd, iso or mkv files in TFS.... (Don't ask... those end-users are "business people" with some development kills requesting access to source control to store "things" they produce...)

V.
5/16/2012 2:41 PM | Valery Letrye
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: