使用Azure DevOps REST API向继承流程工作项类型添加自定义字段报错
问题:Azure DevOps REST API 添加全局自定义字段到继承流程工作项类型失败
我正在使用Azure DevOps REST API自动化继承流程中工作项类型的自定义操作,具体是将现有全局自定义字段(如Custom.Name)添加到继承流程中的特定工作项类型(如Feature)。
调用POST /work/processes/{processId}/workItemTypes/{witRefName}/fields端点时,出现以下错误:
Error: Failed to add field Custom.Name. Status: MethodNotAllowed, Details: {"count":1,"value":{"Message":"The requested resource does not support http method 'POST'."}}
相关代码片段:
定义baseUrl的代码:
string baseUrl = $"https://dev.azure.com/{config.Organization}/_apis";
请求逻辑代码:
string url = $"{baseUrl}/work/processes/{processId}/workItemTypes/{witRefName}/fields?api-version=7.1-preview.1"; Console.WriteLine(url); Console.WriteLine(fieldReferenceName); var data = new { referenceName = fieldReferenceName,//Ex. Custom.Name defaultValue = "", allowGroups = false }; var response = await _client.PostAsync(url, new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json"));
解决方案
该错误的核心原因是目标API端点不支持POST方法。在Azure DevOps REST API中,将全局自定义字段添加到继承流程的工作项类型,需要使用PUT方法发起请求。
修正后的请求代码:
// 仅需将PostAsync替换为PutAsync,其余代码保持不变 var response = await _client.PutAsync(url, new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json"));
补充说明:
- 针对继承流程的工作项类型字段配置操作(添加/更新),Azure DevOps API统一采用PUT方法,POST方法仅适用于创建全新字段的场景
- 请求体中的参数结构无需调整,保持现有定义即可
内容的提问来源于stack exchange,提问作者Pontus
相关产品推荐
相关产品推荐

