Is the TFS REST API that compilcated when using it with stored queries?

半腔热情 提交于 2020-06-28 06:45:06

问题


It is mentioned here: Using the TFS REST API to get all work items in an iteration and I am already working with the VS docs at https://www.visualstudio.com/en-us/docs/integrate/api/wit/queries#get-a-query-or-folder

We try to generate a change log text file based on a stored query on the TFS server. Prior to the REST API we were using the VS 2012/2013 TFS library but are now shifting to REST.

Now by reading the docs I see that when querying work items I should do this for max 200 work items (https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql#get-work-items)

If I understand the documentation correctly, then I need 6 http queries for a stored query that returns 607 work items.

  1. Get Stored Query by name -> http://server/Project/_apis/wit/queries/Shared%20Queries/Change%20Log?api-version=1.0&$expand=all, which returns the WIQL url for the query
  2. Query WIQL -> returning 607 ids
  3. Create a work item query for the first 200 work items
  4. for the next 200
  5. for the next 200
  6. for the next 7

Then assemble those work items and format them according to the display columns configuration which is provided by query no. 1.

By testing the 200 work item limit I found out that the on premise installation actually allow more work items. Currently the max amount of work items is defined by the GET request length of 2096 (tested on TFS 2017).

Is this the confirmed approach on how to execute stored queries?


回答1:


Yes, it is the right approach.

Simple C# code to send REST API:

String MyURI = "[REST API URL]";
            WebRequest WReq = WebRequest.Create(MyURI);
            WReq.Credentials =
                new NetworkCredential("[user name]", "[password]", "[domain]");

            WebResponse response = WReq.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);

On the other hand, you can run the query with WIQL string.

Regarding to get WIQL string of stored query, you can use TFS .net client API.

NetworkCredential cred = new NetworkCredential("[user name]", "[password]", "[domain]");
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("[collection URL"), cred);
            tpc.EnsureAuthenticated();
            WorkItemStore wis = tpc.GetService(typeof(WorkItemStore)) as WorkItemStore;
 QueryHierarchy queryRoot = wis.Projects["[team project]"].QueryHierarchy;
            QueryFolder queryFolder = queryRoot["Shared Queries"] as QueryFolder;
            QueryDefinition qd = queryFolder["PBIS"] as QueryDefinition;
            string tt = qd.QueryText;

Regarding call query REST API with Extended Client package, you can refer to this simple code:

var u = new Uri("[collection url]");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("v-stache", "Hua543@Hua543", "fareast")));
            var connection = new VssConnection(u, c);
            var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();
 var result = workitemClient.QueryByWiqlAsync(new Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.Wiql() { Query = "select[System.Id] from WorkItems where [System.TeamProject] = 'ScrumStarain2' and [System.WorkItemType] = 'Product Backlog Item' and [System.State] <> ''" }, "ScrumStarain2").Result;


来源:https://stackoverflow.com/questions/41163545/is-the-tfs-rest-api-that-compilcated-when-using-it-with-stored-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!