I tried with the following code to get a particular project.
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(_tfs.Uri);
var vsStore = tfs.GetService<VersionControlServer>();
var project = vsStore.TryGetTeamProject(_selectedTeamProject.Name);
Now, I need to get sub folders(if present) for that project. Thanks.
Use the GetItems method
vsStore.GetItems("$/TeamProjectName/*")
And to get more control you can use the ItemSpec property which accepts a wildcard path.
Related
Question background:
I'm trying to do the following process but currently cannot succesfully map the TFS code to the specified local folder:
Create a temporary work space.
Set the temporay workspace to be on the C drive in a folder called 'BasicSccExample'
From a passed object called 'vssItem', concatenate its 'VcQaFolder' and 'Name' properties to set the specified TFS server path.
Map the TFS server path to the local path.
Get the latest code from the TFS server.
Create a folder called 'sub' within the 'BasicSccExample' Folder.
Create the this new 'basic' folder.
Check the differences between the local specified file copies.
Delete the temporary workspace.
The code to carry out the above process:
Workspace workspace = tfServer.CreateWorkspace("BasicSccExample", tfServer.AuthorizedUser);
string topDir = null;
string result = null;
string localDir = #"c:\BasicSccExample";
string test = vssItem.VcQaFolder + "/"+vssItem.Name;
workspace.Map(test, localDir);
workspace.Get();
topDir = Path.Combine(workspace.Folders[0].LocalItem, "sub");
Directory.CreateDirectory(topDir);
var potentialDifference = Difference.DiffFiles(topDir, FileType.Detect(topDir, null), topDir, FileType.Detect(topDir, null), new DiffOptions());
result = potentialDifference.Next == null ? "Identical" : "Different";
workspace.Delete();
The error:
I always receive a null exception when attempting to compare the files - which for testing purpose at the moment are the same file. The reason for this error is because the specified local folder on the C drive that the code should be mapped too is always empty. Can anyone tell me where I'm going wrong?
Sorted this issue out. I was trying to set the file as a folder.
For anyone out there with similar issues, check you're not making the same mistake and this code works
.
I can now map from the specifed TFS server path to the specified local path.
Workspace workspace = tfServer.CreateWorkspace("TempDevWorkspace", tfServer.AuthorizedUser);
string topDir = null;
string localDir = #"c:\TempDevWorkspaceFolder";
workspace.Map(tfsItem.VcDevFolder, localDir);
workspace.Get();
string directory = Path.Combine(workspace.Folders[0].LocalItem, null);
Let's say we have a solution in TFS Source Control which has already been mapped to a local folder SolutionFolder.
We are in a sub folder SubFolder of this SolutionFolder.
How can we write C# code to get the mapped path of this SubFolder?
Use the WorkStation.Current to grab the information for the folder in question:
Import the following namespaces:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
and then use you can get to the data you want through:
var workspace = Workstation.Current.GetLocalWorkspaceInfo(solutionFolder);
if (workspace != null)
{
var teamProjectUri = workspace.ServerUri;
// var server = TfsConfigurationServerFactory.GetConfigurationServer(teamProjectUri);
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(teamProjectUri);
var cssService = projectCollection.GetService<ICommonStructureService4>();
var project = cssService.GetProjectFromName(solutionName);
}
From there you can easily grab the Workspace as well and from there the serverpath: workspace.GetWorkspace().GetServerItemForLocalItem()
To provide credentials, you can use one of the additional overloads that accepts a CredentialsProvider. The default provider is the UICredentialsProvider. Or you can also call server or projectCollection's EnsureAuthenticated.
See also:
https://jessehouwing.net/vsts-tfs-api-auto-detect-connection-details/
It is my first time to work with TFS and I am not quite familiar how workspace mapping works. I would just like to print the local folder and repository folder when the workspace is mapped, so I would know if the workspace is mapped because I get an exception such as "Object reference not set to an instance of an object."
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(path);
var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(path);
And is my Get Latest code correct? I mean I would like to get the latest version of a whole folder and this is what I got.
var service = tpc.GetService<VersionControlServer>();
var getLatestOfTfsRepositoryCs = service.GetWorkspace(#"C:\temp\project");
I suggest you to use Workspace.IsLocalPathMapped method in order to check
link : http://msdn.microsoft.com/fr-fr/library/microsoft.teamfoundation.versioncontrol.client.workspace.islocalpathmapped.aspx
If you wish get lastest version you can use Workspace.Get method, with VersionSpec.Latest value
I created folder with VersionControlServer.CreateTeamProjectFolder:
var connection = new TfsTeamProjectCollection(new Uri("http://my-tfs-serv:8080/tfs/DefaultCollection"));
var vcs = connection.GetService<VersionControlServer>();
vcs.CreateTeamProjectFolder(new TeamProjectFolderOptions("TestFolder"));
That call has created folder $/TestFolder/, now I want to delete it.
I could not find API for that. I tried to remove it with
TFSDeleteProject.exe /force /collection:http://my-tfs-serv:8080/tfs/DefaultCollection TestFolder
But it returns:
TF200016: The following project does not exist: TestFolder. Verify that the name of the project is correct and that the project exists on the specified Team Foundation Server.
Looks like VersionControlServer.CreateTeamProjectFolder() does not create a team project, it just creates the folder only.
But how can i remove that folder now?
TFSDeleteProject cmd won't work on items created with the code you are using. You can use the following code to delete(destroy):
var itemSpec = new ItemSpec(FullRepositoryPath, RecursionType.Full);
Item[] removedItems = TFSImplementor.VersionControl.Destroy(
itemSpec,
VersionSpec.Latest,
null,
DestroyFlags.StartCleanup);
TFSDeleteProject cmd will only work on projects created from Wizard.
I have a project that should be get latest to specific date. I have this code:
var serverFolder = pathInTfs;
var localFolder = pathInLocalMachin;
var workingFolder = new WorkingFolder(serverFolder, localFolder);
// Create a workspace mapping
workspace.CreateMapping(workingFolder);
if (!workspace.HasReadPermission)
{
throw new SecurityException(
String.Format("{0} does not have read permission for {1}",
versionControl.AuthorizedUser, serverFolder));
}
// Get the files from the repository
workspace.Get(dateForLatest, GetOptions.Overwrite);
every thing is good but I want to be get latest only directory "pathInTfs" in "pathInLocalMachin" but when program run workspace.Get() every project be get latest.
How I can get latest one path in my project.
There are several overloads of Get which allow you to specify the set of objects you want to get. For what you're doing, I think you want Get(GetRequest, GetOptions).
The GetRequest includes an ItemSpec where you can specify a folder to get, and then indicate RecursionType.Full.