无法使用 WorkItemTrackingHttpClient 更新 WorkItem 状态

Unable to update WorkItem Status with WorkItemTrackingHttpClient

我正在自动更新工作项小时数,但忽略了对状态的更改。我想将状态从 "Active" 设置为 "Resolved"。

我发现信息表明,如果您要更改状态但我的代码没有更改原因或状态,则您还需要设置 "Reason",尽管所有其他字段更新都在工作。我怀疑这是因为 Status 字段是只读的,但我们无法找到使其成为只读的规则(我们使用的是 CMMI 模板):

有人可以告诉我问题是出在开发操作中的设置还是我的代码(或其他)?

//Executing from LINQPad, no need to mention the blocks on async....

WorkItem targetWorkItem = client.GetWorkItemAsync(123456).Result;    

JsonPatchDocument patchDocument = new JsonPatchDocument();

patchDocument.Add(
    new JsonPatchOperation()
    {
        Operation = Operation.Replace,
        Path = "/fields/Microsoft.VSTS.Scheduling.CompletedWork",
        Value = 123
    }
);

patchDocument.Add(
    new JsonPatchOperation()
    {
        Operation = Operation.Replace,
        Path = "/fields/Microsoft.VSTS.Scheduling.RemainingWork",
        Value = 0
    }
);

/*
    These don't work! I think because "Reason" field is read only
*/
patchDocument.Add(
    new JsonPatchOperation()
    {
        Operation = Operation.Add, //Tried Replace as well as Add
        Path = "/Fields/System.Reason",
        Value = "Complete and Requires Review/Test"
    }

patchDocument.Add(
    new JsonPatchOperation()
    {
        Operation = Operation.Add, //Tried Replace as well as Add
        Path = "/Fields/System.State",
        Value = "Resolved"
    }
);

//Succeeds for any field except Status and Reason
WorkItem result = client.UpdateWorkItemAsync(patchDocument, 123456).Result;

使用的命名空间:

Microsoft.TeamFoundation.WorkItemTracking.WebApi  
Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models  
Microsoft.VisualStudio.Services.Common  
Microsoft.VisualStudio.Services.WebApi  
Microsoft.VisualStudio.Services.WebApi.Patch  
Microsoft.VisualStudio.Services.WebApi.Patch.Json 

你有一个语法错误,你应该用 f/fields/System.State 而不是 FFields

并且改变状态就足够了,原因会自动改变。

您的 Json 应该看起来像这样:

{
     "id": xx,
     "rev": yy,
     "fields": [{
         "field": {
             "refName": "System.State"
         },

         "value": "Resolved"
     },
     {
         "field": {
             "refName": "System.Reason"
         },

         "value": "Status Reason"
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ActivatedBy"
         },

         "value": null
     },

     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ActivatedDate"
         },

         "value": null
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ResolvedDate"
         },

         "value": "2014-08-25T19:14:04.594Z"
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ResolvedBy"
         },

         "value": "User Name"
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ResolvedReason"
         },

         "value": "Resolved Reason"
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ClosedDate"
         },

         "value": <null or "2014-08-25T19:14:04.594Z">
     },
     {
         "field": {
             "refName": "Microsoft.VSTS.Common.ClosedBy"
         },
         "value": <null, "John Doe">
     }]
 }