在上一篇《TFS二次開發01——TeamProjectsPicher》介紹了 TeamProjectsPicher 對象,使用該對象能夠很簡單的實現鏈接TFS。服務器
可是若是咱們要實現自定義的UI客戶端 或者咱們要作一個非WinForm版的TFS客戶端(好比Web 或者WPF),那麼TeamProjectsPicher 對象就無能爲力了。那咱們就只能本身實現了。ui
這篇文章主要介紹:spa
1:鏈接TFS Server3d
2:獲取全部TfsTeamProjectCollection版本控制
3:獲取某個TfsTeamProjectCollection 下的全部Team Projectcode
添加命名空間orm
using Microsoft.TeamFoundation.Client;對象
using Microsoft.TeamFoundation.Framework.Client;blog
using Microsoft.TeamFoundation.Framework.Common;ip
using Microsoft.TeamFoundation.VersionControl.Client;
//想要鏈接TFS,確定先要知道TFS 的地址 string tfsServerURL = "http://192.168.83.70:8080/tfs"; Uri configurationServerUri = new Uri(tfsServerURL); //與TFS服務器鏈接 TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri);
/// <summary> /// 查詢全部指定TFS服務上的全部TeamProjectCollection 信息 /// </summary> /// <param name="tfsServerURL"></param> /// <returns></returns> List<TfsTeamProjectCollection> GetAllTeamProjectCollection(string tfsServerURL) { Uri configurationServerUri = new Uri(tfsServerURL); //與TFS服務器鏈接 TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri); List<TfsTeamProjectCollection> lst = new List<TfsTeamProjectCollection>(); //TFS目錄節點 CatalogNode configurationServerNode = configurationServer.CatalogNode; //查詢當前TFS下的全部TeamProjectCollection ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren( new Guid[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None); //遍歷每個TeamProjectCollection 節點 foreach (CatalogNode tpcNode in tpcNodes) { //獲取 當前 team project collection 名稱. String displayName = tpcNode.Resource.DisplayName; // 得到 當前 team project collection 描述. String description = tpcNode.Resource.Description; //獲取當前 team project collection 的描述. ServiceDefinition tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"]; ILocationService configLocationService = configurationServer.GetService<ILocationService>(); Uri tpcUri = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition)); // 真正的鏈接到team project collection TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tpcUri); lst.Add(tpc); } return lst; }
TfsTeamProjectCollection 對象經常使用屬性:
TfsTeamProjectCollection. CatalogNode. Resource.DisplayName // team project collection 名稱.
TfsTeamProjectCollection .Name // team project collection 名稱.
TfsTeamProjectCollection .Uri // team project collection Url 信息.
TfsTeamProjectCollection. CatalogNode. Resource. Description // team project collection 描述.
TeamProject[] GetAllTeamProject(TfsTeamProjectCollection tpc) { List<TeamProject> lst = new List<TeamProject>(); VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer; return version.GetAllTeamProjects(true); }
因爲咱們在實際應用中鏈接到TFS時,只能鏈接一個TfsTeamProjectCollection 。因此若是咱們知道了該TfsTeamProjectCollection的Url 那麼就能夠免去上面的步驟,代碼乳以下:
/// <summary> /// 獲取全部的TeamProject /// </summary> /// <returns></returns> TeamProject[] GetAllTeamProject(string tpcURL) { //鏈接到team project collection ,使用此方式直接鏈接TFS時 TfsTeamProjectCollection. CatalogNode 爲 none 。 //由於在使用時一般是隻鏈接到一個Team project collection ,全部更多的時候咱們使用這種方式鏈接TFS TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tpcURL)); //版本控制 VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer; //獲取全部TeamProject return version.GetAllTeamProjects(true); }
結合上面的代碼能夠實現以下圖所示的效果:
TeamProjectFolderOptions project = new TeamProjectFolderOptions("project1"); version.CreateTeamProjectFolder(project);