Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS Create new WorkItem

Tags:

azure-devops

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?

like image 756
Thomas Gassmann Avatar asked Jan 15 '16 11:01

Thomas Gassmann


People also ask

How do you create work items?

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.


1 Answers

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;
like image 65
Sean Avatar answered Sep 22 '22 20:09

Sean