在对已删除的工作项进行更新时,需要使用“编辑”操作而非“更新”操作。具体代码示例如下:
WorkItemTrackingHttpClient witClient = connection.GetClient();
JsonPatchDocument patchDocument = new JsonPatchDocument();
patchDocument.Add(new JsonPatchOperation()
{
Operation = Operation.Replace,
Path = "/fields/System.State",
Value = "Active"
});
WorkItem updatedWorkItem = await witClient.UpdateWorkItemAsync(patchDocument, workItemId, null, true);
需要将上述代码中的 Operation = Operation.Replace 改成 Operation = Operation.Test,然后添加如下代码:
// Test the current state of the work item
WorkItem workItem = await witClient.GetWorkItemAsync(workItemId);
if (workItem.Fields["System.State"].ToString() != "Removed")
{
// If the work item is not in the "Removed" state, use the "Update" operation to modify it
updatedWorkItem = await witClient.UpdateWorkItemAsync(patchDocument, workItemId, null, true);
}
else
{
// If the work item is in the "Removed" state, edit it instead of updating it
updatedWorkItem = await witClient.EditWorkItemAsync(patchDocument, workItemId);
}
这段代码会先通过 GetWorkItemAsync 方法获取工作项的当前状态,然后判断状态是否为“删除”("Removed"),若状态为“删除”,则使用 EditWorkItemAsync 方法进行修改,否则使用 UpdateWorkItemAsync 方法进行修改。这样就可以规避 “Field state 'Active' not supported when Work Item has been "Removed"” 这个错误了。