2009-12-01 7 views
12

私はプログラム的にSDKを使用してTFSのうち、ソースコードの最新バージョンを引くしようとしてる、と私は何とかやったことは動作しません: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(); 
    } 
} 

アプローチは基本的に、一時的なワークスペースを作成、使用されますGet()メソッドを使用してこのプロジェクトのすべてのアイテムを取得し、ワークスペースを削除します。これを行う正しい方法ですか?どんな例であれ有用です。

答えて

5

あなたのアプローチは有効です。

エラーはプロジェクトパスにあります。代わりに次のようなものを使用してください:

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

私はあなたのサーバーパスがおそらく原因であるとJoerageに同意します。何が起きているのかをより詳しく知るには、VersionControlServerオブジェクトのイベントをいくつか結びつける必要があります。少なくとも、Getting、NonFatalError、Conflictが必要です。

全リスト:http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver_events(VS.80).aspx

+0

が、変更するとともに、すべてのサポートDLLをコピーしますその道はうまくいかないようでした。私はすべての出来事を配線してみましたが、いずれも解雇されませんでした。私は完全に別の戦略(item.DownloadFile)を使用して終了し、その方法はうまくいくように見えました - そして、ワークスペースは必要ありませんでした。 –

+0

@JohnRasch、同じ問題を抱えているので、コードスニペットを共有してください。 :( – UserAR

10

私は主にItem.DownloadFile()方法を利用して、動作しているように異なるアプローチを使用して終了:

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

私はウェブとボタンにコードを完了し、実装asp.netソリューション。参考文献に働くためのプロジェクトのために

Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.VersionControl.Clientの参照を追加する必要があり、コードにusing Microsoft.TeamFoundation.Client;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

文は、私はA」のコンテンツをダウンロードするために必要な似たような状況がありました'フォルダをtfsから既存のワークスペースにコピーします。上記の回答の助けを借りて、私は今のところ私のためにうまくいくものを一緒に置くことができました。ただし、制限があります。これは、 'a'フォルダの内容に対してのみ機能し、その中の別のフォルダでは機能しません。これは試していません。たぶん、マイナーなアップデートが必要かもしれません。誰かがこれを探している場合に備えて、コードを共有する。私はこのアプローチが仕事場[-createとdelete]を扱っていないという事実が本当に好きです。それは望ましくないからです。

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; 
       } 
      } 
     } 
    } 
} 

コマンドプロンプトからこれを実行している間は、exeファイル CMD >>私は別の問題に巻き込まれたとして、私は応答遅れをお詫びDownloadFolder.exe "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" "C:\MyTempFolder"

関連する問題