我想使用 REST API 在 azure devps 中创建一个字段列表
我遵循以下文档
但是我们没有字符串列表的示例。在字段类型中,我们必须将
string
设置为 list
但我不知道如何设置列表。在 IHM 中,我看到了我的字段,但它是字符串类型。
这是我的 json 请求:
{ "name": "MyField",
"referenceName": "Custom.MyField",
"description": "custom fieldcreate with API",
"type": "string",
"usage": "workItem",
"readOnly": false,
"canSortBy": true,
"isQueryable": true,
"supportedOperations": [
{
"referenceName": "SupportedOperations.Equals",
"name": "="
}
],
"isIdentity": true,
"isPicklist": true,
"isPicklistSuggested": false,
"url": null }
有什么想法吗?
我也尝试选择列表,但我认为这不是解决方案:
{
"id": null,
"name": "picklist_aef2c045-0d2d-4f92-9d09-56eea553e1ef",
"type": "String",
"url": null,
"items": [
"Blue",
"Green",
"Red"
],
"isSuggested": false
}
要使用 REST API 在 Azure DevOps 中创建字段列表,首先,您需要使用 Lists - Create API 创建一个选项列表。
创建选项列表后,您将收到包含选项列表的
id
的响应。您可以使用此 ID 通过 Fields - Create API 在自定义字段中创建选项列表。
这是一个示例 PowerShell 脚本:
# Define variables
$organization = ""
$project = ""
$pat = ""
# Base64 encode the PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
# JSON body for creating the picklist
$picklistJsonBody = @"
{
"name": "MyTestPicklist",
"type": "String",
"items": ["Yellow", "Green", "Red"]
}
"@
# Create the picklist
$picklistResponse = Invoke-RestMethod -Uri "https://dev.azure.com/$organization/_apis/work/processes/lists?api-version=7.1" `
-Method Post `
-Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} `
-Body $picklistJsonBody `
-ContentType "application/json"
$picklistId = $picklistResponse.id
# JSON body for creating the custom field
$fieldJsonBody = @"
{
"name": "MyNewTestField",
"referenceName": "Custom.MyNewTestField",
"description": "Custom field created with API",
"type": "string",
"usage": "workItem",
"readOnly": false,
"canSortBy": true,
"isQueryable": true,
"supportedOperations": [
{
"referenceName": "SupportedOperations.Equals",
"name": "="
}
],
"isPicklist": true,
"picklistId": "$picklistId"
}
"@
# Create the custom field
Invoke-RestMethod -Uri "https://dev.azure.com/$organization/$project/_apis/wit/fields?api-version=7.1" `
-Method Post `
-Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} `
-Body $fieldJsonBody `
-ContentType "application/json"