2009-12-01 4 views
12

Ich bin versucht, die neueste Version des Quellcodes von TFS programmatisch mit dem SDK zu ziehen, und was ich arbeitet irgendwie klappt nicht getan:Wie erhalten Sie die neueste Version des Quellcodes mit dem Team Foundation Server SDK?

string workspaceName = "MyWorkspace"; 
string projectPath = "/TestApp"; 
string workingDirectory = "C:\Projects\Test\TestApp"; 

VersionControlServer sourceControl; // actually instantiated before this method... 

Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name); 
if (workspaces.Length > 0) 
{ 
    sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser); 
} 
Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace"); 
try 
{ 
    workspace.Map(projectPath, workingDirectory); 
    GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest); 
    GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors 
} 
finally 
{ 
    if (workspace != null) 
    { 
     workspace.Delete(); 
    } 
} 

Der Ansatz ist im Grunde einen temporären Arbeitsbereich erstellen, indem die Get() Methode, alle Elemente für dieses Projekt zu greifen und dann den Arbeitsbereich zu entfernen. Ist das der richtige Weg? Jedes Beispiel wäre hilfreich.

Antwort

5

Ihre Vorgehensweise ist gültig.

Ihr Fehler ist in Ihrem Projektpfad. Verwenden Sie stattdessen so etwas:

string projectPath = "$/PathToApp/TestApp"; 
1

Ich stimme mit Joerage überein, dass Ihr Serverpfad wahrscheinlich der Schuldige ist. Um mehr Einblick in die Vorgänge zu erhalten, müssen Sie einige Ereignisse auf dem VersionControlServer-Objekt verbinden. Zumindest wollen Sie Getting, NonFatalError und Conflict.

komplette Liste: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver_events(VS.80).aspx

+0

ich für die Verzögerung in der Antwort entschuldigen, wie ich in einem anderen Problem gefangen wurde, aber zu ändern der Weg schien auch nicht zu funktionieren. Ich habe versucht, alle Ereignisse zu verkabeln, und keiner von ihnen hat gefeuert. Ich habe letztendlich eine andere Strategie verwendet (item.DownloadFile) und dieser Weg schien gut zu funktionieren - und keine Arbeitsbereiche waren notwendig. –

+0

@JohnRasch, Könnten Sie bitte das Code-Snippet teilen, da ich das gleiche Problem habe? !! :( – UserAR

10

landete ich einen anderen Ansatz, der in erster Linie scheint zu funktionieren, die Vorteile der Item.DownloadFile() Methode unter:

VersionControlServer sourceControl; // actually instantiated... 

ItemSet items = sourceControl.GetItems(sourcePath, VersionSpec.Latest, RecursionType.Full); 

foreach (Item item in items.Items) 
{ 
    // build relative path 
    string relativePath = BuildRelativePath(sourcePath, item.ServerItem); 

    switch (item.ItemType) 
    { 
    case ItemType.Any: 
     throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder."); 
    case ItemType.File: 
     item.DownloadFile(Path.Combine(targetPath, relativePath)); 
     break; 
    case ItemType.Folder: 
     Directory.CreateDirectory(Path.Combine(targetPath, relativePath)); 
     break; 
    } 
} 
6

I abgeschlossen und implementiert den Code in eine Schaltfläche als Web asp.net Lösung.

Für das Projekt in den Referenzen arbeiten sollte die Microsoft.TeamFoundation.Client und Microsoft.TeamFoundation.VersionControl.Client Referenzen und im Code die Aussagen using Microsoft.TeamFoundation.Client; und using Microsoft.TeamFoundation.VersionControl.Client;

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string workspaceName = "MyWorkspace"; 
     string projectPath = @"$/TeamProject"; // the container Project (like a tabel in sql/ or like a folder) containing the projects sources in a collection (like a database in sql/ or also like a folder) from TFS 

     string workingDirectory = @"D:\New1"; // local folder where to save projects sources 

     TeamFoundationServer tfs = new TeamFoundationServer("http://test-server:8080/tfs/CollectionName", System.Net.CredentialCache.DefaultCredentials); 
                  // tfs server url including the Collection Name -- CollectionName as the existing name of the collection from the tfs server 
     tfs.EnsureAuthenticated(); 

     VersionControlServer sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer)); 

     Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name); 
     if (workspaces.Length > 0) 
     { 
      sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser); 
     } 
     Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace"); 
     try 
     { 
      workspace.Map(projectPath, workingDirectory); 
      GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest); 
      GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors 
     } 
     finally 
     { 
      if (workspace != null) 
      { 
       workspace.Delete(); 
       Label1.Text = "The Projects have been brought into the Folder " + workingDirectory; 
      } 
     } 
    } 
0

Ich hatte eine ähnliche Situation, wo ich Inhalte von ‚zum Herunterladen einer benötigt hinzugefügt werden 'Ordner von tfs in einen vorhandenen Arbeitsbereich, ohne einen neuen Arbeitsbereich zu erstellen. Mit Hilfe der obigen Antworten konnte ich etwas zusammenfügen, das für mich ab sofort gut funktioniert. Es gibt jedoch eine Einschränkung. Das funktioniert für den Inhalt eines 'a' Ordners mit nur Dateien und nicht einem anderen Ordner darin - das habe ich noch nicht ausprobiert. Vielleicht würde das ein paar kleinere Updates beinhalten. Code teilen, nur für den Fall, dass jemand danach sucht. Ich mag die Tatsache, dass dieser Ansatz sich nicht mit dem Arbeitsbereich beschäftigt [-create und delete], da das nicht gewünscht ist.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Configuration; 
using Microsoft.TeamFoundation.VersionControl.Client; 
using Microsoft.TeamFoundation.Client; 
using System.IO; 

namespace DownloadFolder 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string teamProjectCollectionUrl = "http://<YourTFSUrl>:8080/tfs/DefaultCollection"; // Get the version control server 
      TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl)); 
      VersionControlServer vcs = teamProjectCollection.GetService<VersionControlServer>(); 
      String Sourcepath = args[0]; // The folder path in TFS - "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" 
      String DestinationPath = args[1]; //The folder in local machine - "C:\MyTempFolder" 
      ItemSet items = vcs.GetItems(Sourcepath, VersionSpec.Latest, RecursionType.Full); 
      String FolderName = null; 
      foreach (Item item in items.Items) 
      { 
       String ItemName = Path.GetFileName(item.ServerItem); 
       switch (item.ItemType) 
       { 
        case ItemType.File:       
         item.DownloadFile(Path.Combine(DestinationPath, FolderName, ItemName)); 
         break; 
        case ItemType.Folder: 
         FolderName = Path.GetFileName(item.ServerItem); 
         Directory.CreateDirectory(Path.Combine(DestinationPath, ItemName)); 
         break; 
       } 
      } 
     } 
    } 
} 

Während dies von der Eingabeaufforderung kopieren Sie alle unterstützenden DLLs zusammen mit exe cmd >>DownloadFolder.exe "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" "C:\MyTempFolder"

Verwandte Themen