我们以TFS为前提,我想使用API创建待办事项。
我能够从TFS中获取物品,但是我无法创建。任何指导请
我正在使用以下路径获取物品。
/ tfs / DefaultCollection / _apis / wit / workitems?ids =
因此,在尝试了Postman下面的解决方案后,我得到了。
嗨,
http:// 。。。:8080 / tfs / DefaultCollection / _apis / wit / workitems / $ Product Backlog Items?api-version = 5.0
application / json-patch + json
身体:
所以我尝试了您提到的内容,但出现此错误
<h2>404 - File or directory not found.</h2>
<h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
答案 0 :(得分:0)
您需要提供Request Body
并使用PATCH
方法来创建工作项:
有关详细信息,请参见Create a work item。
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 }
}
}
}
]
您还可以在PowerShell示例下面使用调用REST API的方式创建PBI(根据您的要求添加其他字段):
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)}
更新:
似乎您正在使用不受支持的api-version=5.0
。仅VSTS支持api版本5.x
。对于本地TFS 2017,支持的版本为3.x
,TFS2018为4.x
,并且向前兼容,例如TFS 2017和2018也支持2.x
。
因此,只需尝试使用api-version=2.2
...它就对我有效。
第二个错误表示请求正文格式不正确。您可以按照我的PowerShell示例,仅更改正文的对应值,然后重试。
此外,您可以引用此博客在TFS中创建工作项:Create Work Items in Visual Studio Online with Nintex Workflow 2010