How can I programmatically check-out an item for edit in TFS?
Solution 1
Some of the other approaches mentioned here only work for certain versions of TFS or make use of obsolete methods. If you are receiving a 404, the approach you are using is probably not compatible with your server version.
This approach works on 2005, 2008, and 2010. I don't use TFS any longer, so I haven't tested 2013.
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
{
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(fileName);
}
Solution 2
private const string tfsServer = @"http://tfsserver.org:8080/tfs";
public void CheckOutFromTFS(string fileName)
{
using (TfsTeamProjectCollection pc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer)))
{
if (pc != null)
{
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
if (null != workspaceInfo)
{
Workspace workspace = workspaceInfo.GetWorkspace(pc);
workspace.PendEdit(fileName);
}
}
}
FileInfo fi = new FileInfo(fileName);
}
Note that Microsoft.TeamFoundation.Client.TeamFoundationServerFactory
is obsolete: The TeamFoundationServer
class is obsolete. Use the TeamFoundationProjectCollection
or TfsConfigurationServer
classes to talk to a 2010 Team Foundation Server.
In order to talk to a 2005 or 2008 Team Foundation Server use the TeamFoundationProjectCollection
class. The corresponding factory class for that is the TfsTeamProjectCollectionFactory
.
Solution 3
You can use Team Foundation Version Control client API. The method is PendEdit()
workspace.PendEdit(fileName);
Checkout detailed example on MSDN http://blogs.msdn.com/b/buckh/archive/2006/03/15/552288.aspx
Solution 4
First get the workspace
var tfs = new TeamFoundationServer("http://server:8080/tfs/collection");
var version = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
var workspace = version.GetWorkspace("WORKSPACE-NAME", version.AuthorizedUser);
With the workspace you can checkout the file
workspace.PendEdit(fileName);
Solution 5
var registerdCollection = RegisteredTfsConnections.GetProjectCollections().First();
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(registerdCollection);
var versionControl = projectCollection.GetService<VersionControlServer>();
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(_fileName);
var server = new TeamFoundationServer(workspaceInfo.ServerUri.ToString());
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(fileName);
abatishchev
This is my GUID. There are many like it but this one is mine. My GUID is my best friend. It is my life. I must master it as I must master my life. Without me, my GUID is useless. Without my GUID I am useless.
Updated on July 09, 2022Comments
-
abatishchev almost 2 years
I'm working on a utility processing files being under source control using TFS 2010.
If an item is not yet checked-out for edit, I'm getting an exception, what is definitely predictable because file is in read-only mode.
What ways exist to check-out a file?
P.S. I want something for programmatic rather then
Process.Start("tf.exe", "...");
if that's applicable. -
RandomEngy almost 12 yearsI deleted my answer because yours is a simplified version that works with VS 2010 as well. It turns out you don't have to go through the
TfsConfigurationServer
to get the objects you need. blogs.msdn.com/b/taylaf/archive/2010/02/23/… -
Carl Walsh about 9 years
TfsTeamProjectCollection
is IDisposable, so you might want to wrap it in a using block -
Ben almost 9 yearsYou're right. The code I copied this from kept the
TfsTeamProjectCollection
around for multiple uses. But this improves the example. -
abatishchev almost 8 yearsYou may want to suffix the internal function internal and remove that suffix from the public one.
-
abatishchev almost 8 yearsAlso running sync function in async manner doesn't make much sense to me. Anyway you can probably return task directly rather than await it so it will be awaited caller
-
njkremer over 7 yearsWanted to add a comment for any future visitors. If you're using the more verbose
PendEdit()
function that takes in thesilent
parameter, you want to setsilent
tofalse
, this will make it remove the read-only lock.