Azure DevOps Export Import query to different project

Viewed 1195

Tried searching if there's any out of the box method to export import queries in new projects. Seems like there isn't any.

Can it be achieved by code? If yes, would like to know how.

Appreciate any tips.

Regards,

3 Answers

You can use the rest api to read and create queries: Queries

Powershell example to export queries into local folder:

$user = ""
$token = "<pat>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProject = "team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceQueryFolder = "Shared Queries/Change Management"
$targetLocalFolder = "c:/temp/Change Management Queries"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesUrl = "$orgUrl/$teamProject/_apis/wit/queries/$sourceQueryFolder"+"?`$depth=1&`$expand=all&api-version=5.0"

function InvokeGetRequest ($GetUrl)
{    
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
}

$resQuries = InvokeGetRequest $queriesUrl

if (![System.IO.Directory]::Exists($targetLocalFolder))
{
    New-Item -Path $targetLocalFolder -ItemType "directory"
}

if ($resQuries.isFolder -and $resQuries.hasChildren)
{
    foreach($item in $resQuries.children)
    {
        if (!$item.isFolder)
        {            
            $queryJson = "{`"name`":`"{queryname}`", `"wiql`":`"{querywiql}`"}"

            $queryJson = $queryJson -replace "{queryname}", $item.name
            $queryJson = $queryJson -replace "{querywiql}", $item.wiql

            $filepath = "$targetLocalFolder/" + $item.name

            Set-Content -Path $filepath -Value $queryJson
        }
    }
}

Powershell example to import queries from local folder:

$user = ""
$token = "<pat>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProject = "team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceLocalFolder = "c:/temp/Change Management Queries"
$targetQueryFolder = "Shared Queries/Change Management Imported" #should exist

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesUrl = "$orgUrl/$teamProject/_apis/wit/queries/$targetQueryFolder"+"?&api-version=5.0"

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$files = Get-ChildItem -File -Path $sourceLocalFolder

foreach($wiqlfile in $files)
{
    $wiqlBody = Get-Content $wiqlfile

    InvokePostRequest $queriesUrl $wiqlBody
}

Example to copy a query folder content from one team project into another one in the same organization through rest API:

$user = ""
$token = "<pat>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProjectSource = "source_team_project_name"
$teamProjectTarget = "target_team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceQueryFolder = "Shared Queries/Change Management"
$targetQueryFolder = "Shared Queries/Change Management" #should exist

$queryObject = [PSCustomObject]@{
    name = $null
    wiql = $null
    columns = $null
    sortColumns = $null
}

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesPostUrl = "$orgUrl/$teamProjectTarget/_apis/wit/queries/$targetQueryFolder"+"?api-version=5.0"
$queriesGettUrl = "$orgUrl/$teamProjectSource/_apis/wit/queries/$sourceQueryFolder"+"?`$depth=1&`$expand=all&api-version=5.0"

function InvokeGetRequest ($GetUrl)
{    
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
}

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$resQuries = InvokeGetRequest $queriesGettUrl

if ($resQuries.isFolder -and $resQuries.hasChildren)
{
    foreach($item in $resQuries.children)
    {
        if (!$item.isFolder)
        {   
            $queryObject.name = $item.name 
            $queryObject.wiql = $item.wiql
            $queryObject.columns = $item.columns
            $queryObject.sortcolumns = $item.sortcolumns
            
            $wiqlbody = ConvertTo-Json $queryObject -Depth 10

            InvokePostRequest $queriesPostUrl $wiqlBody
        }
    }
}

You can use out of box function to export and import queries. Visual Studio and Team Explorer contain that.

  1. Go to the Queries Tab:

enter image description here

  1. Edit query

enter image description here

  1. Save as

enter image description here

  1. Select another project

enter image description here

Related