How To Get Azure DevOps Data

We will fetch data using Work Item Query Language (WIQL).
 
WIQL query is like SQL query. The WIQL syntax is not case sensitive. It has the following clauses like:
  • AND
  • OR
  • NOT
  • UNDER
  • SORT
WIQL syntax will never provide you detailed work item information, because to execute this, they use REST API, but that it doesn’t have an endpoint to provide such detailed info.
 
So, to get the detailed info we must follow 2 steps,
  1. Get Ids of work items using “WorkItemQueryResult”, which is the result of "QueryByWiqlAsync(wiql)". Here parameter wiql object consists of our search query.
  2. Once we get ids in “WorkItemReference” which will be IEnumerable type, then we can fetch all details using “GetWorkItemsAsync” method.

Create PAT token

 
To get started first we need a PAT token which is required to communicate with Azure DevOps. PAT is an alternative for password that you can use to authenticate into Azure DevOps. To create it go to Security, from the top left corner menu choose the “New Token”.
 
Then as per your requirement select the options, most of them are self-explanatory. 
 
Important Note
Once you create token, copy it somewhere because it will not be available again.
 
Create C# Console App targeting .NET Framework 4.5.
 
Then add the below package from Nuget,
  1. Microsoft.VisualStudio.Services.Client
  2. Microsoft.TeamFoundationServer.Client
Now in main method use the below code,
  1. Uri orgUrl = new Uri("https://dev.azure.com/{Organization name}/");  
  2. String personalAccessToken = "{pat token}";  
  3. VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));  
Just put your respective values instead of {Organization name}, {pat token} so you can access your DevOps server data.
 
Now we will move on to how to write and execute WIQL and fetch data. For this first we need to create “WorkItemTrackingHttpClient” using,
  1. WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();  
Then create WIQL object and its query like,
  1. Wiql wiql = new Wiql();  
  2.   
  3. wiql.Query = "SELECT * "  
  4. " FROM WorkItems WHERE [System.WorkItemType] in ('Bug','Product Backlog Item')"  
  5. " AND [Microsoft.VSTS.Common.ClosedDate] >'" + Convert.ToDateTime("2019-01-01").ToString() + "'"  
  6. " AND [System.CreatedDate] >'" + Convert.ToDateTime("2019-01-01").ToString() + "'";  
As above I explain we have to use “QueryByWiqlAsync” to execute query, we can do it as,
  1. WorkItemQueryResult tasks = await witClient.QueryByWiqlAsync(wiql);  
  2.   
  3. IEnumerable<WorkItemReference> tasksRefs;  
  4. tasksRefs = tasks.WorkItems.OrderBy(x => x.Id);  
Until this point we get all the work item ids, only now we have to get detailed info using the second  step which I have explained above,
  1. List<WorkItem> tasksList =witClient.GetWorkItemsAsync(tasksRefs.Select(wir => wir.Id)).Result;  
Once you get data in list you can play with it as per your requirement.
 
Note
GetWorkItemsAsync” method has a limit, only take 200 ids at a time.
 
To get detailed info for one work item you have to use for each loop on the above list
 
Then you can access its respective data like,
  1. foreach (var task in tasksList)  
  2. {  
  3.    string wiType = task.Fields["System.WorkItemType"].ToString()  
  4. }  
Mostly work item objects have the below information,
  • System.AreaPath
  • System.TeamProject
  • System.IterationPath
  • System.WorkItemType
  • System.State
  • System.Reason
  • System.CreatedDate
  • System.CreatedBy
  • System.ChangedDate
  • System.ChangedBy
  • System.CommentCount
  • System.Title And so on…