You need to provide the Request Body
and use the PATCH
method to create a work item:
Please see Create a work item for details.
PATCH https://{instance}/DefaultCollection/{project}/_apis/wit/workitems/${workItemTypeName}?api-version={version}
Content-Type: application/json-patch+json
[
{
"op": "add",
"path": { string }
"value": { string or int, depending on the field }
},
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": { string },
"url": { string },
"attributes":
{
{ name/value pairs }
}
}
}
]
You can also use below PowerShell sample calling the REST API to create a PBI (add other fields based on your requirement):
Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "0511ScrumTFVC",
[string]$workItemType = "Product Backlog Item",
[string]$user = "Domain\user",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "0831-PBI-Test"
},
{
"op": "add",
"path": "/fields/System.AreaPath",
"value": "0511ScrumTFVC"
},
{
"op": "add",
"path": "/fields/System.IterationPath",
"value": "0511ScrumTFVC\\Sprint 1"
},
{
"op": "add",
"path": "/fields/System.Tags",
"value": "Tag0831"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.Activity",
"value": "Development"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Scheduling.Effort",
"value": "8"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.ValueArea",
"value": "Business"
}
]
"@
return $value
}
$json = CreateJsonBody
$uri = "$baseurl/$($projectName)/_apis/wit/workitems/"+"$"+"$($workItemType)?api-version=2.2"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
UPDATE:
Seems you are using the unsupported api-version=5.0
. The api version 5.x
is only supported in VSTS. For on-premise TFS 2017 the supported version is 3.x
, TFS2018 is 4.x
, and it's forward compatible, e.g 2.x
is also supported in TFS 2017 and 2018.
So just have a try with the api-version=2.2
... it works on my side.
The second error means the request body format is incorrect. You can follow my PowerShell sample and just change the correspond values for the body, then try it again.
Besides, you can reference this blog to create work items in TFS: Create Work Items in Visual Studio Online with Nintex Workflow 2010