Skip to content

Instantly share code, notes, and snippets.

@MoimHossain
Created November 15, 2023 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoimHossain/1a58dc7bdc0c76fc53604f90b5a48cb1 to your computer and use it in GitHub Desktop.
Save MoimHossain/1a58dc7bdc0c76fc53604f90b5a48cb1 to your computer and use it in GitHub Desktop.
Azure DevOps Query via REST API

AzDO Queries

Because of the depth limit, the following way to retrieve the hierarchy could be a better appraoch.

If you are doing it on client side, you can follow the same algorithm - but only perform the expanson of folders on-demand (responding to user's click on a folder for expansion to prevent any page-freeze)

Load the Root folders

Lets explicitly load without the children (depth = 0)

GET https://{{host}}/{{organization}}/{{project}}/_apis/wit/queries?$depth=0&api-version=7.1-preview.2 
Authorization: {{basicToken}}

This wil give us:

{
    "count": 2,
    "value": [
        {
            "id": "550e5a52-7b88-467a-bcb6-969760220f0b",
            "name": "My Queries",
            "path": "My Queries",
            "createdDate": "2023-11-15T08:11:12.92Z",
            "lastModifiedDate": "2023-11-15T08:11:12.92Z",
            "isFolder": true,
            "hasChildren": true,
            "isPublic": false,
            "url": "https://dev.azure.com/moim/59f493cf-a55d-4ec7-a881-ae9b29bedfca/_apis/wit/queries/550e5a52-7b88-467a-bcb6-969760220f0b"
        },
        {
            "id": "dae28c42-3baf-49a0-a4f6-a58b4d9aa997",
            "name": "Shared Queries",
            "path": "Shared Queries",
            "createdDate": "2023-11-15T08:11:12.573Z",
            "lastModifiedDate": "2023-11-15T08:11:12.573Z",
            "isFolder": true,
            "hasChildren": true,
            "isPublic": true,
            "url": "https://dev.azure.com/moim/59f493cf-a55d-4ec7-a881-ae9b29bedfca/_apis/wit/queries/dae28c42-3baf-49a0-a4f6-a58b4d9aa997"
        }
    ]
}

With that response, we could loop into the sub-folders.

In .net

foreach( var sq in items) {
  if(sq.isFolder && sq.hasChildren) {
    // here you can load the immediate childs for this folder with the folder sq.id
    
  }
}

The following REST API load the immediate children.

@folderId = dae28c42-3baf-49a0-a4f6-a58b4d9aa997

### Get the children for a folder
GET https://{{host}}/{{organization}}/{{project}}/_apis/wit/queries/{{folderId}}?$depth=1
Authorization: {{basicToken}}

Which gives us the following JSON to work with

{
    "id": "dae28c42-3baf-49a0-a4f6-a58b4d9aa997",
    "name": "Shared Queries",
    "path": "Shared Queries",
    "createdDate": "2023-11-15T08:11:12.573Z",
    "lastModifiedDate": "2023-11-15T08:11:12.573Z",
    "isFolder": true,
    "hasChildren": true,
    "children": [
        {
            "id": "50b73416-5667-41b3-8313-bed659649b06",
            "name": "ABC",
            "path": "Shared Queries/ABC",
            "createdDate": "2023-11-15T08:12:29.93Z",
            "lastModifiedDate": "2023-11-15T08:12:29.93Z",
            "isFolder": true,
            "hasChildren": false,
            "isPublic": true
        },
        {
            "id": "11a1308c-5459-4dfe-9cfd-1ec814f3993f",
            "name": "DEF",
            "path": "Shared Queries/DEF",
            "createdDate": "2023-11-15T08:12:40.74Z",
            "lastModifiedDate": "2023-11-15T08:12:40.74Z",
            "isFolder": true,
            "hasChildren": true,
            "isPublic": true
        }
    ],
    "isPublic": true
}

From this response we can return the children array and continue the loop altogether.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment