Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
Creating a new bug (or any work item) is pretty straight forward. You just need to set the field values and send a JSON-Patch object to the REST endpoint.
Have the following before you can create a work item.
To learn about C# programming within Visual Studio, find the Visual Studio C# programming documentation
There are a few things happening in the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi;
using System.Net.Http.Headers;
using System.Net.Http;
using Newtonsoft.Json;
public class CreateBug
{
readonly string _uri;
readonly string _personalAccessToken;
readonly string _project;
/// <summary>
/// Constructor. Manually set values to match your organization.
/// </summary>
public CreateBug()
{
_uri = "https://dev.azure.com/{orgName}";
_personalAccessToken = "personal access token";
_project = "project name";
}
/// <summary>
/// Create a bug using the .NET client library
/// </summary>
/// <returns>Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem</returns>
public WorkItem CreateBugUsingClientLib()
{
Uri uri = new Uri(_uri);
string personalAccessToken = _personalAccessToken;
string project = _project;
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
JsonPatchDocument patchDocument = new JsonPatchDocument();
//add fields and their values to your patch document
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = "Authorization Errors"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",
Value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http:// msdn.microsoft.com/library/live/hh826547.aspx"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.Common.Priority",
Value = "1"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.Common.Severity",
Value = "2 - High"
}
);
VssConnection connection = new VssConnection(uri, credentials);
WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
try
{
WorkItem result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, "Bug").Result;
Console.WriteLine("Bug Successfully Created: Bug #{0}", result.Id);
return result;
}
catch (AggregateException ex)
{
Console.WriteLine("Error creating bug: {0}", ex.InnerException.Message);
return null;
}
}
}
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create a .NET Aspire project - Training
Learn how to create cloud-native applications from scratch or add orchestration to an existing app by using the .NET Aspire stack in .NET 8.
Documentation
Get work items programmatically from Azure DevOps Services - Azure DevOps
Use REST APIs to get work items from Azure DevOps Services with queries in your own custom apps.
REST API samples - Azure DevOps
REST API samples for Azure DevOps, including personal access tokens (PATs).
Get started with the REST APIs for Azure DevOps - Azure DevOps
Learn the basic patterns for using the REST APIs for Azure DevOps.