如何获取所有Team Services帐户中的所有工作项?

时间:2017-05-29 15:05:28

标签: tfs azure-devops object-model azure-devops-rest-api

我正在使用.NET库来访问Visual Studio Team Services,而我正试图解决微软的一个明显的设计缺陷。显然,每个服务器/帐户不能有多个集合,因此我必须使用多个帐户,在此示例中我将其称为集合,因为Microsoft甚至已明确they map to the same thing

我实际想要实现的是拥有一个列表,其中列出了我所属的所有集合中的所有工作项。我有一个QueryWorkItems()方法,它使用GetAllCollections()来获取我的所有集合。该方法已经过测试并且有效,它确实返回了我拥有的两个帐户。触发整个事物的顶级方法是AssignedWorkItems()。我的代码如下:

        public static List<TfsTeamProjectCollection> GetAllCollections()
        {
            // Get collections/accounts associated with user
            string request = "https://app.vssps.visualstudio.com/_apis/Accounts?memberId=" + versionControl.AuthorizedIdentity.TeamFoundationId + "&api-version=3.2-preview";
            string content = MakeRequestToAPI(request).Result;
            dynamic results = JsonConvert.DeserializeObject<dynamic>(content);
            List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>();
            // Iterate through all collections
            Parallel.ForEach((IEnumerable<dynamic>)results.value, collection =>
           {
               TfsTeamProjectCollection col = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri((string)collection.accountUri));
               collections.Add(col);
           });
            return collections;
        }


        public static List<WorkItem> QueryWorkItems(string query)
        {
            List<WorkItem> workItems = new List<WorkItem>();
            List<TfsTeamProjectCollection> collections = GetAllCollections();
            //Parallel.ForEach(collections, collection =>
            foreach(var collection in collections)
            {
                WorkItemCollection items = collection.GetService<WorkItemStore>().Query(query);
                // Add each work item to the overall list
                Parallel.For(0, items.Count, i =>
                {
                    Console.WriteLine(items[i].Title);
                    lock (workItems)
                    {
                        workItems.Add(items[i]);
                    }
                });
            }

            return workItems;
        }

        public static List<WorkItem> AssignedWorkItems()
        {
            Init(); //initializes variables like projectName, workItemStore and VersionControlServer(versionControl)
            string myItems = "select * from issue where [System.AssignedTo]=@me";
            return QueryWorkItems(myItems);
        }

当我调用AssignedWorkItems方法时,即使我已经设置了默认连接,我也会收到登录提示:enter image description here

我输入凭据后,在这一行:

  

WorkItemCollection items = collection.GetService()。Query(query);

我收到以下错误:

  

“Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException”类型的未处理异常   发生在Microsoft.TeamFoundation.Client.dll

中      

其他信息:TF31002:无法连接到此团队   Foundation Server:https://xxxxxx.vssps.visualstudio.com/

     

Team Foundation Server网址:https://xxxxxx.vssps.visualstudio.com/

     

失败的可能原因包括:

     
      
  • Team Foundation Server的名称,端口号或协议不正确。

  •   
  • Team Foundation Server处于脱机状态。

  •   
  • 密码已过期或不正确。

  •   

有趣的是每次我运行这个错误时提到的URL在我拥有的两个集合之间来回切换。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我可以成功测试 XMLHttpRequest cannot load https://blockchain.info/rawaddr/1N1WJYDUgaBrk9eUfawSYurs9ZtKcVVfTE. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 方法。

根据您收到的错误消息,VSTS网址似乎存储在QueryWorkItems中,格式为https://account.vssps.visualstudio.com,而不是https://account.visualstudio.com。因此,请确认 collections 方法中存储在集合中的网址是否正确。

我使用此 GetAllCollections 方法验证 GetAllCollections

QueryWorkItems
相关问题