Geeks With Blogs

News




View Tarun Arora's profile on LinkedIn

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

Tarun Arora - Visual Studio ALM MVP ALM, Agile, Automation, Performance Testing, Software QA, Cloud, ...

 

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 :
Posted on Tuesday, August 30, 2011 7:54 PM TFS2010 , TFS API | Back to top


Comments on this post: TFS SDK: Workspace Explorer & Statistics

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Emil Cristen on Sep 06, 2011 8:21 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Tarun Arora on Sep 07, 2011 1:28 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by kiquenet on Sep 20, 2011 10:45 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Tarun Arora on Sep 20, 2011 10:59 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Very good work. Can you please send me working copy of your current solution?

Thanks in advance.
Left by Hitesh on Dec 01, 2011 7:12 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Tarun Arora on Dec 01, 2011 9:49 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Very interesting and useful! I'd be very much obliged if you could send me the source code.
Left by H. Noell on Dec 16, 2011 6:41 PM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Tarun Arora on Dec 17, 2011 10:05 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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.
Left by S. Lavoie on Jan 04, 2012 5:32 PM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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
Left by Tarun Arora on Jan 04, 2012 6:47 PM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Thanks for the article.
Can I see the full source code? (it would be very useful)

WBR,
Kirill Temnenkov
Left by Kirill Temnenkov on Apr 12, 2012 8:53 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
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.
Left by Valery Letrye on May 16, 2012 2:41 PM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Hi Tarun,

Yet another very interesting post!.
If possible could you send the solution, I want to try more things with it.

Thanks,
Nagaraj
Left by Nagaraj on Jun 29, 2012 6:56 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Hi Nagaraj,

Thanks for your feedback.
Don't have the solution handy, but i will email you the solution over the weekend.

Mean while, I tend to generally upload my projects to the skydrive here, https://skydrive.live.com/#cid=61D0A67D27B527D3&id=61D0A67D27B527D3%21152

Regards,
Tarun
Left by Tarun Arora on Jun 29, 2012 9:04 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Hi Tarun,

I'am impressed of your solution. Can you share me the code?

Regards,
Lieven
Left by Lieven on Aug 24, 2012 8:33 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Hi Tarun,

Very good articles. it helping me to build TFS framework.Please send me a working copy of your solution.

Thanks
Sukumar
Left by Sukumar on Oct 05, 2012 10:05 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Hi Tarun,

your article seems to me very interesting. I am trying to figure out how the TFS object model works.

Could you send me a copy of the code to understand better how it works. thanks in advance.

pd. I looked in your shared folder in skydrive, but the code wasnt there.

Regards,
Left by orojas on Oct 31, 2012 12:25 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Could you please share or send me the copy of demo source code for this application.That would be great!

thanks in advance.

Regards,
MAyur
Left by Mayur on Dec 18, 2012 6:56 AM

# re: TFS SDK: Workspace Explorer & Statistics
Requesting Gravatar...
Could you please send me working copy of your current solution on this project?

I have checked on your skyDrive shared projects.didnt find thsi project.
Left by Mayur on Dec 19, 2012 9:57 AM

comments powered by Disqus

Copyright © Tarun Arora [Microsoft MVP] | Powered by: GeeksWithBlogs.net