Home > other >  How to create a task or user story via the rest api
How to create a task or user story via the rest api

Time:03-02

I am trying to create a user story and task in Azure DevOps with this RESTapi There are multiple backlogs.

POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=6.0

My code

function Set-pbiStuff {
    param
    ( 
        [Parameter(Mandatory = $true)] [string] $Organization,
        [Parameter(Mandatory = $true)] [string] $Project,
        [Parameter(Mandatory = $true)] [hashtable] $Token
    )
  
    $Base = "https://dev.azure.com/$($organization)/$($project)/_apis/wit/workitems"
    $workItemType = 'task'
    $URL = "$($Base)/$($workItemType)?api-version=6.0"
    $Json = @(
        @{
            op    = 'add'
            path  = '/fields/System.Title'
            value = $workItemType
        }
    )
    
    $Body = (ConvertTo-Json $Json)
  
    $response = Invoke-RestMethod `
        -Method Post `
        -Uri $URL `
        -ContentType 'application/json' `
        -Body $Body `
        -Headers $Token
    Write-Host $URL
    Write-Host $response
}

$Token= @{Authorization = 'Basic '   [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($env:SYSTEM_ACCESSTOKEN)")) }

$tt = Set-pbiStuff -Organization 'myOrganization' -Project 'myProject' -Token $Token

return $tt

But the response I get is that the page was not found. What have I missed?

CodePudding user response:

You're trying to call the API with a PATCH verb, see line two:

$response = Invoke-RestMethod `
    -Method Patch `
    -Uri $URL `
    -ContentType 'application/json' `
    -Body $Body `
    -Headers $AzAuthHeader

The API endpoint is, like the documentation shows and you stated in your question, a POST endpoint.

CodePudding user response:

Try this URL:

$URL = "$($Base)/`$$($workItemType)?api-version=6.0"

Check this sample: https://arindamhazra.com/create-azure-devops-task-using-powershell/

  • Related