使用TFS BasicAuthCredentials获取项目

时间:2013-10-14 02:37:42

标签: tfs azure-devops tfs-sdk

我正在尝试从TFS Online获取项目列表。我没有得到任何异常,但tpcNodes集合计数始终为0,因此它不会列出任何项目。

public List<string> GetProjects()
        {
            string _myUri = "https://testredrock.visualstudio.com/defaultcollection";
            List<string> projects = new List<string>();
            NetworkCredential netCred = new NetworkCredential("userName", "password");
            BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
            TfsClientCredentials credential = new TfsClientCredentials(basicCred);
            credential.AllowInteractive = false;
            string TFSServerPath = host;

            using (TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(TFSServerPath), credential))
            {
                tfs.EnsureAuthenticated();

                CatalogNode catalogNode = tfs.CatalogNode;
                ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(
                                new Guid[] { CatalogResourceTypes.ProjectCollection },
                                false, CatalogQueryOptions.None);
                foreach (CatalogNode tpcNode in tpcNodes)
                {
                    Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
                    // Get catalog of tp = 'Team Projects' for the tpc = 'Team Project Collection'
                    var tpNodes = tpcNode.QueryChildren(
                              new Guid[] { CatalogResourceTypes.TeamProject },
                              false, CatalogQueryOptions.None);
                    foreach (var p in tpNodes)
                    {
                        projects.Add(p.Resource.DisplayName);
                    }
                }
            }
            return projects;
        }

1 个答案:

答案 0 :(得分:0)

我建议您使用TfsTeamProjectCollectionFactory.GetTeamProjectCollection来访问您的项目。