如何以编程方式在 TFS 中创建错误

How to create a bug in TFS Programmatically

我正在寻找一个小代码片段,它可以帮助我使用 C# VSTS2015 以编程方式在 TFS 中创建缺陷。

我的 TFS 服务器:http://tfs2.dell.com:8080/tfs/eDell/eDellPrograms/。 服务器是:http://tfs2.dell.com:8080/tfs。 Collection是:eDell。 项目是:eDellPrograms.

工作项 = 缺陷。

您有 2 个选择:

  1. TFS 肥皂 API(SDK)
  2. TFS 休息 API

对于 TFS Soap API (SDK),您需要这些 DLL:

Microsoft.TeamFoundation.Client;
Microsoft.TeamFoundation.WorkItemTracking.Client;

密码是:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace createNewWorkItem
{
    class Program
    {
        static int Main(string[] args)
        {
            Uri collectionUri = new Uri("http://server:8080/TFS/");
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);
            WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
            Project teamProject = workItemStore.Projects["MyProject"];
            WorkItemType workItemType = teamProject.WorkItemTypes["Defect"];

            WorkItem Defect = new WorkItem(workItemType);

            Defect.Title = "TITLE GOES HERE";
            Defect.Description = "DESCRIPTION GOES HERE";
            Defect.Fields["Issue ID"].Value = "999999";


            Defect.Save();
            return (Defect.Id);

        }
    }

}

如果您想使用 Rest API,则不需要上述 DLL。

密码是:

public static async void createtWorkItem()
{
        string requestUrl = "http://TFS2015servername:8080/tfs/{collectionname}/{teamprojectname}/_apis/wit/workitems/$Defect?api-version=1.0";
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(new object[]{new
        {
            op = "add",
            path = "/fields/System.Title",
            value = "New Task from TFS 2015 REST API"
        }});

        HttpClientHandler authtHandler = new HttpClientHandler()
        {
           // Credentials = CredentialCache.DefaultNetworkCredentials
           Credentials = new NetworkCredential("username", "password", "domainname")
        };

        using (HttpClient client = new HttpClient(authtHandler))
        {
            var method = new HttpMethod("PATCH");

            var request = new HttpRequestMessage(method, requestUrl)
            {
                Content = new StringContent(json, Encoding.UTF8,
                    "application/json-patch+json")
            };
        HttpResponseMessage hrm = await client.SendAsync(request);
        }

        Console.WriteLine("Completed!");
    };
}