Search code examples
azure-devopsazure-devops-rest-api

How to edit a field value with Azure DevOps API REST


I am trying to edit a field of an existing work item.

I want to edit the current value adding new substring after the old one.

What I do is get the current value:

https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.0&fields=My.Field.Name

Getting this:

{
    "id": {id},
    "rev": 1,
    "fields": {
        "My.Field.Name": "ABC"
    },
}

And then, what I don't know is how to modify it, for example to update it with the new value: "ABCDEF"

Any clue?


Solution

  • You'd issue a PATCH request on the work item with the value to change:

    PATCH https://dev.azure.com/fabrikam/_apis/wit/workitems/{id}?api-version=7.0
    
    [
      {
        "op": "test",
        "path": "/rev",
        "value": 1
      },
      {
        "op": "add",
        "path": "/fields/My.Field.Name",
        "value": "ABCDEFG"
      }
    ]
    

    The first op: test is optional. It tests whether the work item was updated by someone else while you were preparing to modify it. It tests whether the revision is still what is was when you retrieved the values.

    If you leave out the test, it will overwrite the value regardless of whether someone else had changed it.

    PATCH https://dev.azure.com/fabrikam/_apis/wit/workitems/{id}?api-version=7.0
    
    [
      {
        "op": "add",
        "path": "/fields/My.Field.Name",
        "value": "ABCDEFG"
      }
    ]