Thursday, February 15, 2007

How to check if file is already in TFS repository?

Recently I have come across one development issue with version control object model. In development of VS TFS add-in, it was essential to check whether some file is already controlled (that is added to source control repository).

It is tempting to use TryGetServerItemForLocalItem method of Workspace class - if the item does not exist on server, it will return empty string. But nothing in the life is that simple - all GetServerItem/TryGetServerItem (as well as GetLocalItemForServerItem and TryGetLocalItemForServerItem) do not test items as to existence in the repository; in fact they are very thin wrapper over simple parsing methods.

Then naturally there must be something in VersionControlServer class, right? When I started looking for that method, I come across some pretty weird solutions (like using GetItem method - and in case exception is thrown, then item does not exist).
But in fact there is special method that does exactly that - best solution for finding out if item exists at server. It is (not too) aptly named ServerItemExists and it even has two overrides: first one is simple check whether item exists by path and item type, while the second one allows to supply additionally item version and deleted status.

So be advanced and use appropriate method! And thumbs up to Microsoft for good design of object model!

No comments: