2

I am automating a TFS branching process with DotNet core 2.2. We are using TFS 2018.

How can I create a new branch in TFS source control using C#?

I have been able to queue a build a do some other operations.

Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer has a method for CreateBranch but I have not been able to instantiate a VersionControlServer. Old documentation shows this is done with TfsTeamProjectCollection but I can't find any libraries with that class.

A lot of the MSDN documentation goes to obsolete or deprecated references.

UPDATE I have found that TfsTeamProjectCollection is in MicrosoftTeamFoundationServer.ExtendedClient if I install version 14.83.0, but the current version is 16.143.2.
When I drop back to this old version I get runtime errors associated with versions of other packages. If I downgrade them there are several build errors so I will need to refactor. I can't downgrade, and shouldn't have to.

What has replaced this?

Don Chambers
  • 3,798
  • 9
  • 33
  • 74
  • Related to https://stackoverflow.com/questions/12835228/cannot-seem-to-use-tfs-api-how-to-correctly-import-items-from-the-sdk – riQQ Aug 07 '19 at 09:11

2 Answers2

2

Update

For the test from my side, It works properly with .net framework project.

But not able to do with .net Core, it's even not able to restore related Nuget packages.


Yes, it's using namespace Microsoft.TeamFoundation.VersionControl.Client and Assembly: Microsoft.TeamFoundation.VersionControl.Client

Sample code for your reference:

var server = new TfsTeamProjectCollection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
var vcServer = server.GetService<VersionControlServer>();

var changesetId = vcServer.CreateBranch("$/Demo/Main","$/Demo/Dev/Branch", VersionSpec.Latest, null, "Branch Comment", null, null, null);

You could see the official documentation for this class at: VersionControlServer Class

To get this Microsoft.TeamFoundation.VersionControl.Client.dll, it has been been moved into a Nuget package--Microsoft.TeamFoundationServer.ExtendedClient.

Take a look at this similar question here: Where can I find Microsoft.TeamFoundation.VersionControl.Client.dll in Visual Studio 2015 installation?

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Same as the response from Shayki Abramczyk. This no longer works with the last few versions of the library. – Don Chambers Aug 07 '19 at 18:26
  • @DonChambers Please see my update. For the test from my side, It works properly with .net framework project. But not able to do with .net Core, it's even not able to restore related Nuget packages. How did you restore/install this package with .net core? We got an error with"Severity Code Description Project File Line Suppression State Error Package restore failed. Rolling back package changes for 'ConsoleApp1'. " – PatrickLu-MSFT Aug 08 '19 at 10:25
  • 1
    That worked. Using the dotnetFramework 4.7.2 libraries it worked as expected. I will just make this a dotNetFramework. – Don Chambers Aug 08 '19 at 15:05
0

You can use MicrosoftTeamFoundationServer.ExtendedClient NuGet package, then you will have all the classes and methods:

var tfs = new TfsTeamProjectCollection(new Uri("tfs-server-url"));
tfs.Authenticate();
var versionControl = tfs.GetService<VersionControlServer>();
versionControl.CreateBranch("sourcePath","targetPath", VersionSpec.Latest);   
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • Same answer as above, and the same thing I find. But it seems that TfsTeamProjectCollection is not in that package. – Don Chambers Aug 07 '19 at 13:38
  • Try to add `using Microsoft.TeamFoundation.VersionControl.Client` – Shayki Abramczyk Aug 07 '19 at 13:43
  • Got it. TfsTeamProjectCollection is in the Microsoft.TeamFoundation.Client namespace from the MicrosoftTeamFoundationServer.ExtendedClient nuget package. But, it's only in version 14. It's gone ins 15 and 16. It also seems to be gone in the last 14 release. I am not sure what we should use going forward. – Don Chambers Aug 07 '19 at 14:09
  • Nice catch! I don't know why MS not give this ability in the new Rest API Libraries :/ – Shayki Abramczyk Aug 07 '19 at 14:21