通过API在团队服务中创建工作项 [英] Create Work Item in Team Services Through API

查看:86
本文介绍了通过API在团队服务中创建工作项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为客户端创建一个Web应用程序(在Azure上托管),以便能够将工作项提交到我们的团队服务页面.基本上是一个支持票证页面,因此他们不必一直打电话来解释他们的积压情况.

I'm trying to create a web app (hosted on Azure) for clients to be able to submit work items to our team services page. Basically a support ticket page so they don't have to call to explain their backlog all the time.

下面是我按照Microsoft的示例代码创建工作项所使用的类和方法,出于隐私方面的考虑,进行了一些明显的更改.该方法是通过单击按钮触发的,到目前为止,我无法创建任何工作项.

Below is the class and method i've made to create work items, following Microsoft's sample code, with some obvious changes for privacy reasons. This method is triggered by a button click, and so far I cannot get it to create any work items.

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

 namespace customapp
 {
  public class CreateWorkItem
  {


    public void CreateWorkItemMethod()
    {

        string personalAccessToken = "xxxxxxxxx";
        string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "xxx", personalAccessToken)));

        Object[] patchDocument = new Object[1];

        patchDocument[0] = new { op = "add", path = "/fields/System.Title", value = "Test" };


        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);


            var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");

            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, "https://example.visualstudio.com/exampleproject/_apis/wit/workitems/$Support&20Ticket?api-version=1.0") { Content = patchValue };
            var response = client.SendAsync(request).Result;


            if (response.IsSuccessStatusCode)
            {
               var result = response.Content.ReadAsStringAsync().Result;

            }

 }}}} 

在PATCH的URL中,我使用团队项目的ID(代替您在下面看到的/exampleproject).我们的站点设置为有一个整体项目,我们称其为"Master",而内部则是每个客户的团队项目,例如"ClientProject".因此,我基本上想在Master中创建一个"Support Ticket"工作项. -> ClientProject->积压/委员会.

In the url for the PATCH I am using the team project's ID (in place of /exampleproject you see below). Our site is set up to have an overall project, let's call it ""Master", and inside is a team project for each client, for example "ClientProject". So basically I want to create a "Support Ticket" work item in Master->ClientProject->Backlog/Board.

推荐答案

改为使用Master\\areapath(而不是Master\areapath).

样本主体:

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "PBIAPI2"
  },
  {
    "op": "add",
    "path": "/fields/System.AreaPath",
    "value": "Scrum2015\\SharedArea"
  }
]

另一方面,最好通过将VSTS/TFS API与 Microsoft Team Foundation Server扩展客户端软件包.

On the other hand, it’s better to create work item by using VSTS/TFS API with Microsoft Team Foundation Server Extended Client package.

简单的示例代码:

var u = new Uri("https://[account].visualstudio.com");
            VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));
           var connection = new VssConnection(u, c);

var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();
var workitemtype = "Product Backlog Item";
            string teamProjectName = "Scrum2015";
            var document = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();
            document.Add(
    new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
    {
        Path = "/fields/Microsoft.VSTS.Common.Discipline",
        Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
        Value = "development"
    });
            document.Add(
                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
                {
                    Path = "/fields/System.Title",
                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                    Value = string.Format("{0} {1}", "RESTAPI", 6)
                });
            document.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
            {
                Path = "/fields/System.AreaPath",
                Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                Value =string.Format("{0}\\{1}",teamProjectName, "SharedArea")
            });
            document.Add(
                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
                {
                    Path = "/fields/System.AssignedTo",
                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                    Value = "[user account]"
                });
            document.Add(
                new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
                {
                    Path = "/fields/System.Description",
                    Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                    Value = "destest"
                });
var workitem= workitemClient.CreateWorkItemAsync(document, teamProjectName, workitemtype).Result;

这篇关于通过API在团队服务中创建工作项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆