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

问题描述 投票:0回答:1

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

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

工作项=缺陷。

c# api tfs tfs2012 defects
1个回答
0
投票

你有2个选择:

  1. TFS Soap API(SDK)
  2. TFS Rest 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!");
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.