I am trying to create an automation tool to get the latest code from TFS. I need to check if any workspace with the same name exists in the system. If an instance of the workspace exists. Otherwise, create a workspace and mappings.
I found that I Microsoft.TeamFoundation.VersionControl.ClientVersionControlServerhave a method Workspace GetWorkspace(string workspaceName, string workspaceOwner);for getting an existing workspace. But this will throw an exception if the workspace does not exist in the system.
So give me a code that checks for the existence of a workspace and mappings.
I currently have the following code that I know, its the wrong way
try
{
workspace = versionControl.GetWorkspace(workspaceName, versionControl.AuthorizedUser);
versionControl.DeleteWorkspace(workspaceName, versionControl.AuthorizedUser);
workspace = null;
}
catch (Exception e)
{
DialogResult res = MessageBox.Show("There are no workspace mapped. I am creating a new workspace mapped to your local folder named DevFolder.", "Error", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
{
return;
}
}
if (workspace == null)
{
var teamProjects = new List<TeamProject>(versionControl.GetAllTeamProjects(false));
if (teamProjects.Count < 1)
{
MessageBox.Show("Please select a team project.");
return;
}
workspace = versionControl.CreateWorkspace(workspaceName, versionControl.AuthorizedUser);
ReCreateWorkSpaceMappings(workspace);
createdWorkspace = true;
}