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

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

问题描述

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

以下是我创建工作项的类和方法,遵循 Microsoft 的示例代码,出于隐私原因做了一些明显的更改.此方法由单击按钮触发,到目前为止我无法使用它来创建任何工作项.

使用系统;使用 System.Net.Http;使用 System.Net.Http.Headers;使用 System.Text;使用 Newtonsoft.Json;命名空间自定义应用程序{公共类 CreateWorkItem{public void CreateWorkItemMethod(){字符串personalAccessToken = "xxxxxxxxx";字符串凭据 = 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" };使用 (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;如果(响应.IsSuccessStatusCode){var 结果 = response.Content.ReadAsStringAsync().Result;}}}}}

在 PATCH 的 url 中,我使用了团队项目的 ID(代替您在下面看到的/exampleproject).我们的站点设置了一个整体项目,我们称之为Master",里面是每个客户的团队项目,例如ClientProject".所以基本上我想在Master中创建一个Support Ticket"工作项->ClientProject->Backlog/Board.

解决方案

使用 Master\areapath 代替(而不是 Masterareapath).

示例正文:

<预><代码>[{"op": "添加","path": "/fields/System.Title",值":PBIAPI2"},{"op": "添加","path": "/fields/System.AreaPath","value": "Scrum2015\SharedArea"}]

另一方面,最好使用带有 Microsoft Team Foundation Server 扩展客户端包.

简单示例代码:

var u = new Uri("https://[account].visualstudio.com");VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[个人访问令牌]"));var connection = new VssConnection(u, c);var workitemClient = connection.GetClient();var workitemtype = "Product Backlog Item";string teamProjectName = "Scrum2015";var document = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();文档.添加(新 Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation(){路径 = "/fields/Microsoft.VSTS.Common.Discipline",操作 = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,价值=发展"});文档.添加(新 Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation(){Path = "/fields/System.Title",操作 = 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",操作 = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,Value =string.Format("{0}\{1}",teamProjectName, "SharedArea")});文档.添加(新 Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation(){Path = "/fields/System.AssignedTo",操作 = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,值 = "[用户帐户]"});文档.添加(新 Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation(){Path = "/fields/System.Description",操作 = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,值 = "destest"});var workitem= workitemClient.CreateWorkItemAsync(document, teamProjectName, workitemtype).Result;

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.

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;

            }

 }}}} 

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.

解决方案

Using Master\areapath instead (not Masterareapath).

Sample body:

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

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

Simple sample code:

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 在 Team Services 中创建工作项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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