Create a bug in Azure DevOps Services using .NET client libraries
TFS 2018
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.
Prerequisites
You must have the following before you can create a work item.
- An organization in Azure DevOps Services. If you don't have one, you can create one for free
- A Personal Access Token, find out how to create one
- A C# development environment, you can use Visual Studio
Create a C# project in Visual Studio
To learn about C# programming within Visual Studio, find the Visual Studio C# programming documentation
C# code content
There are a few things happening in the following code sample:
- Authentication
- Creating credentials using your PAT
- Creating a VSSConnection with your Azure DevOps Services URI and the credentials
- Retrieving the client using your VSSConnection
- Creating the bug
- Create an array of objects to set the field values
- Convert that array to a serialized json object
- Send that serialized json object to the REST endpoint
C# code snippet
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;
}
}
}
Next steps
- Check out another Quickstart: Get a list of work items using queries
- Explore the integrate samples