I'm using the VSTS REST API and I'm trying to create a new WorkItem. But I'm only able to get an existing WorkItem from VSTS and update the WorkItem.
var listDoNotUpdate = new List<string>();
listDoNotUpdate.Add("System.BoardColumn");
listDoNotUpdate.Add("System.BoardColumnDone");
var wi = await this.Client.GetWorkItemAsync(4000);
wi.Fields["System.Title"] = "Test";
wi.Fields["System.Description"] = "Test";
wi.Fields["Microsoft.VSTS.Common.AcceptanceCriteria"] = "Test";
var doc = new JsonPatchDocument();
foreach (var field in wi.Fields)
{
if (!listDoNotUpdate.Contains(field.Key))
{
doc.Add(new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Replace,
Path = string.Concat("/fields/", field.Key),
Value = field.Value
});
}
}
await this.Client.UpdateWorkItemAsync(doc, 4000);
But how can I create a new WorkItem and upload this?
Procedure. Click Work Items in the main menu, and, in the Create Work Items section, select the type of work item to create. Alternatively, use the shortcut on the Welcome to Work Items page. Click Create a work item, select the project area, and then select the work item type.
You're close. Instead of calling UpdateWorkItemAsync you need to call UpdateWorkItemTemplateAsync.
var collectionUri = "https://{account}.visualstudio.com";
var teamProjectName = "{project}";
var workItemType = "{workItemType}";
var client = new WorkItemTrackingHttpClient(new Uri(collectionUri), new VssClientCredentials());
var document = new JsonPatchDocument();
document.Add(
new JsonPatchOperation()
{
Path = "/fields/System.Title",
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Value = "Title"
});
var wi = client.UpdateWorkItemTemplateAsync(
document,
teamProjectName,
workItemType).Result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With