locked
Create a Branch Programmatically from C# RRS feed

  • Question

  • Hi,

    I've been tasked to create work items and branches in VSTS from within our C# application. I've seen many examples for creating a work item and I was able to do it myself using the libraries (Microsoft.VisualStudio.Services.Client & Microsoft.TeamFoundationServer.Client). So far I've only seen one example creating a branch in TFS 2013, but I could not follow along since the CreateBranch method is now obsolete (according to the documentation). I was also unable to find the TeamFoundationServer class in these libraries.

    public void CreateBranchWithComment(string serverUrl, string sourcePath, string targetPath, string comment)
    {
      var tfs = new TeamFoundationServer(serverUrl);
      var vcServer = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
    
      var changesetId = vcServer.CreateBranch(sourcePath, targetPath, VersionSpec.Latest);
    
      var changeset = vcServer.GetChangeset(changesetId);
      changeset.Comment = comment;
      changeset.Update();
    }

    Is it still possible to create branches via the REST API or libraries? If so, could you please point me in the right direction?

    Tuesday, February 13, 2018 9:30 AM

Answers

  • It turns out that this functionality is only available from the SOAP methods. I missed the TfsTeamProjectCollection class when I read the documentation. Because I was solely focusing on REST API, I paid little attention to anything related to the SOAP methods and examples.

    According to the documentation the TeamFoundationServer class is obsolete since Visual Studio 2010, however the CreateBranch method is still available in the VersionControlServer class.

    To retrieve an instance of the VersionControlServer class, you must call GetService from a TfsTeamProjectCollection instance. Include the Microsoft.TeamFoundationServer.ExtendedClient Nuget package.

    For Example:

    var server = new TfsTeamProjectCollection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
    var vcServer = server.GetService<VersionControlServer>();
    
    /*
     * See documentation for this class at:
     * https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver(v=vs.120).aspx
     */
    var changesetId = vcServer.CreateBranch("$/Demo/Main","$/Demo/Dev/Branch", VersionSpec.Latest, null, "Branch Comment", null, null, null);

    See the TfsTeamProjectCollection class documentation for more information.

    • Marked as answer by Curlack Wednesday, February 14, 2018 10:41 AM
    Wednesday, February 14, 2018 6:08 AM

All replies

  • It turns out that this functionality is only available from the SOAP methods. I missed the TfsTeamProjectCollection class when I read the documentation. Because I was solely focusing on REST API, I paid little attention to anything related to the SOAP methods and examples.

    According to the documentation the TeamFoundationServer class is obsolete since Visual Studio 2010, however the CreateBranch method is still available in the VersionControlServer class.

    To retrieve an instance of the VersionControlServer class, you must call GetService from a TfsTeamProjectCollection instance. Include the Microsoft.TeamFoundationServer.ExtendedClient Nuget package.

    For Example:

    var server = new TfsTeamProjectCollection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
    var vcServer = server.GetService<VersionControlServer>();
    
    /*
     * See documentation for this class at:
     * https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver(v=vs.120).aspx
     */
    var changesetId = vcServer.CreateBranch("$/Demo/Main","$/Demo/Dev/Branch", VersionSpec.Latest, null, "Branch Comment", null, null, null);

    See the TfsTeamProjectCollection class documentation for more information.

    • Marked as answer by Curlack Wednesday, February 14, 2018 10:41 AM
    Wednesday, February 14, 2018 6:08 AM
  • Hi Curlack,

    This forum discusses and asks questions about extending and integrating with Visual Studio, since your issue is more related to vsts, I would suggest that you could post on stackoverfolow with vsts tag for suitable support.

    https://stackoverflow.com/questions/tagged/vsts

    Thanks for your understanding and cooperation.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 14, 2018 6:11 AM
  • No problem. Thanks for the reply. I will do so.
    Wednesday, February 14, 2018 10:43 AM