0
votes

I'm trying to create the same queries across several projects. I feel like I'm almost there, but can't come up with the right 'thing' to create more than one query. I've tried "," ";" "AND" - and have managed to get it to create one but never both.

Could someone assist?? I've put down a quick example snippet of the type of thing I'm trying (which is why the fields are the same). If I can get that right, I'll use it for real with all the different queries I have (there's quite a few!)

{    $url1="https://dev.azure.com/[ORGANISATION]/$($project)/_apis/wit/queries/Shared Queries?api-version=6.0"
   

    $JSON = @'
    {
      "name": "All User Stories JW6", "wiql": "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State],[System.Tags],[Microsoft.VSTS.Scheduling.OriginalEstimate],[Microsoft.VSTS.Scheduling.CompletedWork] FROM workitems WHERE [System.WorkItemType] = 'User Story' AND [System.State] = 'Active' ORDER BY [System.WorkItemType]",
      "name": "All User Stories JW7", "wiql": "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State],[System.Tags],[Microsoft.VSTS.Scheduling.OriginalEstimate],[Microsoft.VSTS.Scheduling.CompletedWork] FROM workitems WHERE [System.WorkItemType] = 'User Story' AND [System.State] = 'Active' ORDER BY [System.WorkItemType]"
    }
'@
1
How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue? - Leo Liu-MSFT

1 Answers

1
votes

Create several queries across multiple projects in Azure DevOps

I am afraid we could not set duplicate Keys in JSON body.

That because the names in json object SHOULD be unique, check the state in RFC-7159:

The current standard for JSON published by the Internet Engineering Task Force (IETF), states "The names within an object SHOULD be unique".

I use the body in the postman, I got the same warning:

enter image description here

If we use the duplicate Keys in JSON body, most popular parsers may take only the last value present in the object for a particular key and ignore the previous ones.

In your sample, only query All User Stories JW7 will be created.

To resolve this issue, we have to create a another body $JSON2, and send another request:

$JSON2 = @'
{
  "name": "All User Stories JW7", "wiql": "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State],[System.Tags],[Microsoft.VSTS.Scheduling.OriginalEstimate],[Microsoft.VSTS.Scheduling.CompletedWork] FROM workitems WHERE [System.WorkItemType] = 'User Story' AND [System.State] = 'Active' ORDER BY [System.WorkItemType]"
}
'@

$CreateQuery1= Invoke-RestMethod -Uri $url -headers $headers -Method Post -ContentType "application/json" -Body $Json1

$CreateQuery2= Invoke-RestMethod -Uri $url -headers $headers -Method Post -ContentType "application/json" -Body $Json2