我正在寻找一个小代码片段,它可以帮助我使用C#VSTS2015在TFS中以编程方式创建缺陷。
我的TFS服务器:http://tfs2.dell.com:8080/tfs/eDell/eDellPrograms/.服务器是:http://tfs2.dell.com:8080/tfs.集合为:eDell。项目是:eDellPrograms。
工作项=缺陷。
您有两个选项:
- TFS Soap API(SDK(
- 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!");
};
}