Get TFS mapped folder of a local sub folder of the solution? - c#

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/

Related

Rename TFS Branch Programaticly c# (TFS2017)

I'm trying to rename a branch programaticly using "PendRename" method from Microsoft.TeamFoundation.VersionControl.Client.
Moves working, so, if I use this to move everything to a new location, it works, but what I need is to rename a branch.
It is possible to do this by commandline "tfs.exe rename " (even this is given me errors if I have more than one workspace mapped for the same server url. the ... could not be found in your workspace, or you do not have permission to access it.)
So, could you please help to understand why rename a branch is not working?
Thank you,
To Rename or move files and folders in TFVC, you must be one of the Contributors for your team project. See Team Foundation Server default groups, permissions, and roles.
I tested that and renamed the branch correctly with the "PendRename" method.
Below code sample for your reference:
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace RenameBranch
{
class Program
{
static void Main(string[] args)
{
string oldPath = #"E:\andy\0418Scrum\web0418-0823";
string newPath = #"E:\andy\0418Scrum\web0418-1020";
string collection = #"http://server:8080/tfs/DefaultCollection";
var tfsServer = new Uri(collection);
var tpc = new TfsTeamProjectCollection(tfsServer);
var vcs = tpc.GetService<VersionControlServer>();
Workspace workspace = vcs.GetWorkspace("YourWorkspaceName", vcs.AuthorizedUser);
workspace.PendRename(oldPath, newPath);
}
}
}
Then you need CheckIn it of course. Use a "workspace.GetPendingChanges()" and "workspace.CheckIn()" methods to do it.
Thank you,
The problem was, there are different processes that are generating workspaces to the directory, and then, he can't remove it.
So, I solve this issue doing this steps:
1. reserve a workspace local location for my appĀ“
2. see if this workspace is mapped. If yes, I remove it
3. create a workspace to the previous folder in path and create the map
4. get latest
5. rename
Thank you Andy-MSFT for your support.

How to get sub folders in TFS programmatically?

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.

TFS API: Mapped worked space is always null

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);

How to map workspace and get latest folders in c#

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

get latest specific project in C#

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.

Categories

Resources