Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Work Item using Azure DevOps Rest API using C#

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?

like image 903
Sandeep Dhamale Avatar asked Apr 13 '20 15:04

Sandeep Dhamale


People also ask

How do I use REST API with Azure DevOps?

First, provide API URL to get list of project. This URL needs to have the DevOps organization. Move to the Authorization section, sect Type as Basic Auth and provide the PAT Token to the Password field. Once done, send the request, You will have JSON Response of all the Projects.

How do I use Azure DevOps REST API with Postman?

Enter in to the current (not initial) variables your Azure DevOps organization name: Organization, AccessToken ad Api-version. Select Update. Close the Manage Environments dialog. IMPORTANT: It is not recommended to use production user accounts as this information is stored directly in Postman.

Does Azure DevOps have a REST API?

Welcome to the Azure DevOps Services/Azure DevOps Server REST API Reference. Representational State Transfer (REST) APIs are service endpoints that support sets of HTTP operations (methods), which provide create, retrieve, update, or delete access to the service's resources.


1 Answers

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://docs.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

like image 190
Shamrai Aleksander Avatar answered Sep 23 '22 18:09

Shamrai Aleksander