Create Work Item in Team Services Through API Create Work Item in Team Services Through API azure azure

Create Work Item in Team Services Through API


Using Master\\areapath instead (not Master\areapath).

Sample body:

[  {    "op": "add",    "path": "/fields/System.Title",    "value": "PBIAPI2"  },  {    "op": "add",    "path": "/fields/System.AreaPath",    "value": "Scrum2015\\SharedArea"  }]

On the other hand, it’s better to create work item by using VSTS/TFS API with Microsoft Team Foundation Server Extended Client package.

Simple sample code:

var u = new Uri("https://[account].visualstudio.com");            VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));           var connection = new VssConnection(u, c);var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();var workitemtype = "Product Backlog Item";            string teamProjectName = "Scrum2015";            var document = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();            document.Add(    new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()    {        Path = "/fields/Microsoft.VSTS.Common.Discipline",        Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,        Value = "development"    });            document.Add(                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()                {                    Path = "/fields/System.Title",                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,                    Value = string.Format("{0} {1}", "RESTAPI", 6)                });            document.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()            {                Path = "/fields/System.AreaPath",                Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,                Value =string.Format("{0}\\{1}",teamProjectName, "SharedArea")            });            document.Add(                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()                {                    Path = "/fields/System.AssignedTo",                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,                    Value = "[user account]"                });            document.Add(                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()                {                    Path = "/fields/System.Description",                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,                    Value = "destest"                });var workitem= workitemClient.CreateWorkItemAsync(document, teamProjectName, workitemtype).Result;


The method must be POST and Uri correct for consume the Api tfs is :

https://dev.azure.com/{organization}/{proyect}/_apis/wit/workitems/${type}?api-version=5.0

Check in :

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.0

The next code function for me.

 static void Main(string[] args)    {        CreateWorkItem();    }    public static void CreateWorkItem()    {        string _tokenAccess = "************"; //Click in security and get Token and give full access https://azure.microsoft.com/en-us/services/devops/        string type = "Bug";        string organization = "type your organization";        string proyect = "type your proyect";        string _UrlServiceCreate = $"https://dev.azure.com/{organization}/{proyect}/_apis/wit/workitems/${type}?api-version=5.0";        dynamic WorkItem = new List<dynamic>() {                new                {                    op = "add",                    path = "/fields/System.Title",                    value = "Sample Bug test"                }            };        var WorkItemValue = new StringContent(JsonConvert.SerializeObject(WorkItem), Encoding.UTF8, "application/json-patch+json");        var JsonResultWorkItemCreated = HttpPost(_UrlServiceCreate, _tokenAccess, WorkItemValue);    }    public static string HttpPost(string urlService, string token, StringContent postValue)    {        try        {            string request = string.Empty;            using (HttpClient httpClient = new HttpClient())            {                httpClient.DefaultRequestHeaders.Accept.Clear();                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", token))));                using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("POST"), urlService) { Content = postValue })                {                    var httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result;                    if (httpResponseMessage.IsSuccessStatusCode)                        request = httpResponseMessage.Content.ReadAsStringAsync().Result;                }            }            return request;        }        catch (Exception ex)        {            throw new Exception(ex.Message);        }    }