get latest specific project in C# - 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.

Related

How to get recent build TFS programmatically?

Ok i am new to TFS. I want to get the recent build pro grammatically using c# code. i have seen other post about this, they did this way
var buildDefinitions = buildServer.QueryBuildDefinitions("Project_name").Where(bd=>bd.DateCreated.Date>=DateTime.Now.AddDays(-7).Date);
But I need to get an example of this image click here
As you know, the method returns IBuildDefinitions. And since you seem to be wanting to find them by name, you could do something like:
var buildDefinitions = buildServer
.QueryBuildDefinitions("Project_name")
.Where(bd => bd.Name == yourBuildName);

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.

TFS ISubscriber plugin, how do I access the files checked in?

I am writing a plugin for TFS that performs automatic branching and merging based on an xml file stored in source. I am able to perform this on the server except I am unable to get the latest of the xml file if it was changed.
public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties)
{
TeamFoundationVersionControlService versionControl = requestContext.GetService<TeamFoundationVersionControlService>();
string localTempFile = Path.GetTempFileName();
versionControl.DownloadFile(requestContext, serverItemPath, 0, VersionSpec.Parse("C" + versionControl.GetLatestChangeset(requestContext).ToString(), null).First(), localTempFile);
return EventNotificationStatus.ActionApproved
}
The issue is that because I want to intercept the checkin before it becomes a changeset, the download file function gets me the version of the latest checkin, not the version that was promoted. Does anyone know how to get the version being checked in?
You should try the artifacts field of the CheckinEvent class.
CheckinEvent ev = notificationEventArgs as CheckinEvent;
I would take a different route: use a CI build that monitors just that file.
It is way more robust and manageable than a server plug-in.

Get TFS mapped folder of a local sub folder of the solution?

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/

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

Categories

Resources