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, ...

 

 

Working as a TFS Administrator maintaining the user workspaces is a very important task. Using the TFS API it is very easy to get a list of user workspaces, undo pending changes and delete workspace mappings if required. It is common to see people leave as the bell curve dips with the project delivery but some times what is left behind is a set of checked out files in workspaces. In this blog post i will show you how to use the TFS API to programmatically get the user workspaces and details related to the workspace and then undo any pending changes in the workspace and finally delete the workspace mapping. I have a blog post that shows you from where to download the VS 2010 SP1 SDK and how to connect to TFS programmatically.

I will show you how to do this,

  • Using TFS API or
  • Using TFS Command Line or
  • Using TFS Side kicks

Lets start with TFS API, this is how the demo application interface looks like,

Download Demo Application

image

 

1. Get List of user workspaces programmatically filtering by user or computer name

public static Workspace[] GetWorkspace(string workspaceName, string userName, string computerName)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConfigurationManager.AppSettings["TfsUri"]));
            var service = tfs.GetService<VersionControlServer>();

            return service.QueryWorkspaces(string.IsNullOrEmpty(workspaceName) ? null : workspaceName, 
                                            string.IsNullOrEmpty(userName) ? null : userName, 
                                            string.IsNullOrEmpty(computerName) ? null : computerName);
        }

Note - If you pass a NULL the query will return a list of all available workspaces in the team project collection.

image

2. Get a list of all pending changes in a workspace programmatically

For the selected workspace, use the GetPendingChanges() method to get a list of all pending changes in that workspace.

public static PendingChange[] GetPendingChangesInTheWorkspace(string workspaceName, string userName, string compName)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConfigurationManager.AppSettings["TfsUri"]));
            var service = tfs.GetService<VersionControlServer>();

            Workspace workspace = service.QueryWorkspaces(string.IsNullOrEmpty(workspaceName) ? null : workspaceName, 
                                                            string.IsNullOrEmpty(userName) ? null : userName, 
                                                            string.IsNullOrEmpty(compName) ? null : compName).First();

            var pendingchanges = workspace.GetPendingChanges();

            return pendingchanges;
        }

 

image

3. Undo pending changes in a user workspace programmatically

For the selected workspace, get all the pending changes using GetPendingChanges() method and then use the Undo method to undo all those checked out files.

public static void UndoPendingChanges(string workspaceName, string userName, string compName)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConfigurationManager.AppSettings["TfsUri"]));
            var service = tfs.GetService<VersionControlServer>();

            Workspace workspace = service.QueryWorkspaces(string.IsNullOrEmpty(workspaceName) ? null : workspaceName, 
                                                            string.IsNullOrEmpty(userName) ? null : userName, 
                                                            string.IsNullOrEmpty(compName) ? null : compName).First();
            
            var pendingChanges = workspace.GetPendingChanges();
            
            workspace.Undo(pendingChanges);
        }

 

4. Using TFS Command Line Utility to get a list of user workspaces 

But if you did not want to use the TFS API, TFS provides some very useful command line utilities you can use to do the above.

image


tf workspaces [/owner:ownername] [/computer:computername] [/collection:TeamProjectCollectionUrl] [/format:(brief|detailed)] [/updateUserName:oldUserName] [/updateComputerName:oldComputerName] [workspacename][/login:username,[password]]



Specify "*" to match workspaces created by any user, computer, workspace name

 

Sample: To Get a list of all workspaces in a Team Project Collection use the query below,

c:\projects>tf workspaces /owner:* /computer:* /collection:http://myserver:8080/tfs/DefaultCollection

image

Sample: Get detailed information about a users workspace such as the working folders, list of checked out files, etc

c:\projects> tf workspace /format: detailed  /owner:* /computer:* /collection:http://myserver:8080/tfs/DefaultCollection

image

Sample: Remove all cached workspaces from the cache in the team project collection at the address http://myserver:8080/tfs/DefaultCollection.

c:\projects>tf workspaces /remove:* /collection:http://myserver:8080/tfs/DefaultCollection

5. Using TFS Command Line Utility to delete a users workspace

You can use the TFS command line utility ‘Workspace’ to create or delete user workspace.

image

tf workspace /new [/noprompt] [/template:workspacename[;workspaceowner]] [/computer:computername] [/comment:("comment"|@comment file)] [/collection:TeamProjectCollectionUrl] [/permission:(Private|PublicLimited|Public)][workspacename[;workspaceowner]][/login:username,[password]]
tf workspace /delete [/collection:TeamProjectCollectionUrl] workspacename[;workspaceowner][/login:username,[password]]

 

Sample: The following command removes the user ‘Beta’ workspace from the server

c:\projects>tf workspace /delete Beta1

6. Using TFS Command Line Utility to undo pending changes

You can use the TFS command line utility ‘Undo’ to undo pending changes in a user workspace. You must be the owner of the specified workspace or have the global Administer workspaces permission set to Allow to execute this command successfully.

image
tf undo [/workspace:workspacename[;workspaceowner]] [/recursive] itemspec [/noprompt] [/login:username, [password]] [/collection:TeamProjectCollectionUrl]

 

Sample: The following removes the pending change for the specified file in ‘Tarun’ ‘Win7’ workspace. I will be using the recursive parameter to undo all pending changes under that path.

c:\projects>tf undo /workspace:WIN7;Tarun $/Main /recursive

7. What other options do i have?

Well if you didn’t want to use the TFS API and did not want to use the command line utility, you could use ‘TFS Sidekicks’, download the latest version from => http://www.attrice.info/cm/tfs/ Team Foundation Sidekicks is a suite of tools for Microsoft Team Foundation Server administrators and advanced users providing Graphic User Interface for administrative and advanced version control tasks in multi-user TFS environments.

What tools are you using for Administration?

Digg This
Posted on Saturday, July 16, 2011 8:39 PM TFS2010 , TFS API , TFS Utilties | Back to top


Comments on this post: TFS 2010 SDK: Get List of user Workspaces and checked out files Programmatically

comments powered by Disqus

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