简体   繁体   中英

Creating workitem in Azure DevOps using REST API - Status 404 not found

I have been trying to create work items in Azure DevOps. But got an error of "404 not found"

ItemType is -Task with $ in prefix. When I try to get the specific work item number in the url (get request) I get other items, so I know for sure that the org + project and the rest of the URI is correct.

What could be the issue? (I gave full access permissions as well when generated the token (PAT))

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

namespace TaskCreator
{
    static class Azure
    {
        public const string BASE = "https://dev.azure.com";
        public const string PAT = "XXX";
        public const string ORG = "Org2";
        public const string API = "api-version=6.0";
        public const string PROJECT = "Project2";
        public const string WIT_TYPE = "$Task";
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create and initialize HttpClient instance.
            HttpClient client = new HttpClient();

            // Set Media Type of Response.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Generate base64 encoded authorization header.
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", Azure.PAT))));

            // Build the URI for creating Work Item.
            string uri = String.Join("?", String.Join("/", Azure.BASE, Azure.ORG, Azure.PROJECT, "_apis/wit/workitems", Azure.WIT_TYPE), Azure.API);

            // Create Request body in JSON format.
            string json = "[{ \"op\": \"add\", \"path\": \"/fields/System.Title\", \"from\": null, \"value\": \"REST API Demo task\"}]";
            HttpContent content = new StringContent(json, Encoding.UTF8, "application/json-patch+json");

            // Call CreateWIT method.
            string result = CreateWIT(client, uri, content).Result;

            // Pretty print the JSON if result not empty or null.
            if (!String.IsNullOrEmpty(result))
            {
                dynamic wit = JsonConvert.DeserializeObject<object>(result);
                Console.WriteLine(JsonConvert.SerializeObject(wit, Formatting.Indented));
            }

            // Presss any key to exit
            Console.ReadLine();
            client.Dispose();
        }

        public static async Task<string> CreateWIT(HttpClient client, string uri, HttpContent content)
        {
            try
            {
                // Send asynchronous POST request.
                using (HttpResponseMessage response = await client.PostAsync(uri, content))
                {
                    response.EnsureSuccessStatusCode();
                    return (await response.Content.ReadAsStringAsync());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return string.Empty;
            }
        } // End of CreateWIT method
    }
}

From UI, you can check whether you are able to create task work item. I encountered a similar behavior when the Task workitem was greyed out at the UI level.

Also, check the code with other workitem types. (for instance bug)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM