Search code examples
azure-devopsazure-devops-rest-apiazure-devops-extensions

Azure DevOps queryByWiql does not query correct project


I would like to know if there is a way for the Azure DevOps dashboard widget to know what project it belongs to or resides in. For example, I have 2 projects. The widget should be able to differentiate between the two, fetch different data, but ultimately do the same thing.

I took a look at the API reference

Here is what I tried:

var projectId = VSS.getWebContext().project.id;
var query = {
    query: "SELECT [System.Id] FROM WorkItem WHERE [System.WorkItemType] = 'Epic' AND [System.State] NOT IN ('Closed','Completed','Resolved','Removed', 'Done')"
};

witClient.queryByWiql(query, projectId).then(
    function (epics) {
      epics.workItems.forEach(epic => {
          ...

However, I am getting back Epics from projects that the dashboard is not under. Our org has several projects/products that have their own WorkItems.

I verified that the projectId variable maps to the correct project, I just don't know why my query returns Epics that do not belong to the correct project...

I want to create a widget that determines which project it belongs to, and fetch data for only that project.


Solution

  • When you want to have the data of a specific project, be sure that you also refer to it in the WIQL query. You can do this by adding [System.TeamProject] = @project to your query.

    var projectId = VSS.getWebContext().project.id;
    var query = "SELECT [System.Id] FROM WorkItem WHERE [System.WorkItemType] = 'Epic' AND [System.State] NOT IN ('Closed','Completed','Resolved','Removed', 'Done') AND [System.TeamProject] = @project";
    
    witClient.queryByWiql({ query: query }, projectId).then(function (epics) {
        // do your stuff here
    });