7

I am unable to create Work Item using Azure DevOps REST API as mentioned in Work Items - Create

Request:

https://dev.azure.com/{organization}/MyTestProject/_apis/wit/workitems/$Task?api-version=6.0-preview.3

Request Body:
[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "Task2"
  }
]

Code to Get Response (Note this code works for all other POST Requests):

using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
      {
         response.EnsureSuccessStatusCode();
         JsonResponse = await response.Content.ReadAsStringAsync();
      }

Response: 400

Can someone please suggest?

1
  • 1
    Did you try to add the "from": null element to the JSON, as outlined in the documentation? Did you set the content-type to application/json-patch+json, as indicated in the documentation? Commented Apr 13, 2020 at 15:38

2 Answers 2

9

It might be helpful to see your full example. However, here is a working example with Newtonsoft.Json (do not forget to create your PAT create personal access token):

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string PAT = "<personal access token>"; //https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
            string requestUrl = "https://dev.azure.com/<my_org>/<my_project>/_apis/wit/workitems/$Task?api-version=5.0";
            try
            {
                List<Object> flds = new List<Object>
                {
                    new { op = "add", path = "/fields/System.Title", value = "Title" }
                };


                string json = JsonConvert.SerializeObject(flds);

                HttpClientHandler _httpclienthndlr = new HttpClientHandler();

                using (HttpClient client = new HttpClient(_httpclienthndlr))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", PAT))));


                    var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl)
                    {
                        Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
                    };

                    HttpResponseMessage responseMessage = client.SendAsync(request).Result;
                }

            }
            catch (Exception ex)
            {

            }
        }
    }
}

Additionally, you can consider to use .NET client libraries for Azure DevOps and TFS. Here is the example: Create a bug in Azure DevOps Services using .NET client libraries

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Shamrai, here's a similar question you might be able to answer. Thank you! stackoverflow.com/questions/64771092/…
How can we add custom fileds?
6

application/json-patch+json is required.

1 Comment

Much appreciate you share the solution here. It would much better if you can accept this so that others could directly know this worked solution:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.