VSTS创建新的WorkItem

时间:2016-01-15 11:26:49

标签: azure-devops

我正在使用VSTS REST API而我正在尝试创建一个新的WorkItem。但是我只能从VSTS获取现有的WorkItem并更新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);

但是如何创建一个新的WorkItem并上传它呢?

4 个答案:

答案 0 :(得分:5)

你很亲密。您需要调用UpdateWorkItemTemplateAsync。

,而不是调用UpdateWorkItemAsync
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;

答案 1 :(得分:3)

似乎他们忘记了CreateWorkItemAsync方法。所有其他Update方法都有相应的Create方法。 无论如何要创建工作项,您可以使用以下代码段

var client = new RestClient(string.Format(
    CultureInfo.InvariantCulture,
    "https://{0}.visualstudio.com/defaultcollection/{1}/_apis/",
    "<vstsAccount>",
    "<project>"));
client.Authenticator = new HttpBasicAuthenticator("accessToken", "<accessToken>");
var json = @"[{'op': 'add','path': '/fields/System.Title','value': 'Title of your work item'}]";
var request = new RestRequest("wit/workitems/$Product Backlog Item?api-version=1.0", Method.PATCH);
request.AddParameter("application/json-patch+json", json, ParameterType.RequestBody);
request.AddHeader("Accept", "application/json");
var response = client.Execute(request);

响应将包含新工作项的json。用它来提取新项目的ID。

答案 2 :(得分:1)

您是否尝试使用&#34;添加&#34;操作而不是&#34;替换&#34;?

另见https://www.visualstudio.com/integrate/api/wit/work-items#Createaworkitem

答案 3 :(得分:0)

至少从v16.135.0版本开始(可能更早),WorkItemTrackingHttpClient对象上有一个CreateWorkItemAsync。

    //
    // Summary:
    //     [Preview API] Creates a single work item.
    //
    // Parameters:
    //   document:
    //     The JSON Patch document representing the work item
    //
    //   project:
    //     Project ID
    //
    //   type:
    //     The work item type of the work item to create
    //
    //   validateOnly:
    //     Indicate if you only want to validate the changes without saving the work item
    //
    //   bypassRules:
    //     Do not enforce the work item type rules on this update
    //
    //   suppressNotifications:
    //     Do not fire any notifications for this change
    //
    //   userState:
    //
    //   cancellationToken:
    //     The cancellation token to cancel operation.
    public virtual Task<WorkItem> CreateWorkItemAsync(JsonPatchDocument document, Guid project, String type, Boolean? validateOnly = null, Boolean? bypassRules = null, Boolean? suppressNotifications = null, Object userState = null, CancellationToken cancellationToken = default(CancellationToken));