Search code examples
azure-devops-rest-apiworkitem

How to set a work item state while creating or updating work item in Azure Devops Rest API?


I have been working to create an API which programatically creates/updates work item in Azure Devops. I have been able to create a work item and populate almost all fields. I have problem with setting the state.

When I am creating a POST request to Azure Devops rest api with any state name like "Active", "Closed", "Rejected", it throws a 400 Bad Request error.

I don't know if I am missing anything or if there something wrong with the way I am trying to set the value.

{
  "op" : "add", 
  "path": "/fields/System.State", 
  "value"="Active",
}

Solution

  • I have found the solution to this problem and hence I am answering it here.

    I was getting a 400 Bad Request error whenever I tried Creating an Item and Setting the state in the same call. I debugged the code and caught the exception. I found out that, there are some validation rules for some of the fields. State is one of them.

    The rule for System.State field is, whenever a Work Item is created it takes its configured default value. ( In my case it was "Proposed", in your case it could be "New"). If you try altering the value at the time of work item creation, it throws a 400 Bad Request.

    What should I do if I have to Create a Work Item with a specific State?

    As of now, the solution that I have found out is to make two calls. One for Work Item Creation and another for Changing the state of the work item to desired state.

    CreateWorkItem()
    {
     var result = await _client.Post(url, jsonData);
     var result2 = await _client.Put(result.id, jsonData); // or maybe just the state
     return Ok(result2);
    }