Search code examples
c#tfsazure-devopsazure-devops-rest-apitfs-sdk

Unable to update WorkItem Status with WorkItemTrackingHttpClient


I am automating updates to work item hours but changes to Status are ignored. I'd like to set status from "Active" to "Resolved".

I have found information stating that you also need to set a "Reason" if you are changing the status but my code isn't changing Reason or Status although all the other field updates are working. I suspect it is because the Status field is read-only but we are unable to find a rule that makes it so (we're using the CMMI template):

enter image description here

Can someone tell me if the problem is the setup in dev ops or if it is my code (or something else)?

//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;

Namespaces used:

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 

Solution

  • You have a syntax error, you should write /fields/System.State with f and not Fields with F.

    And change the state is enough, the reason will be changed automatically.