如何在VSO 2015中向工作项添加历史记录和附件? 到目前为止,我已成功登录,运行查询并从查询结果中检索工作项。 但是,工作项目对象没有历史或附件的属性:
WorkItemTrackingHttpClient witClient = new WorkItemTrackingHttpClient(new Uri(collectionUri), new VssAadCredential("first.last@blablabla.com", "secret"));
List<QueryHierarchyItem> items = witClient.GetQueriesAsync(teamProjectName, QueryExpand.All,2).Result;
QueryHierarchyItem myQueriesFolder = items.FirstOrDefault(qhi => qhi.Name.Equals("Shared Queries"));
if (myQueriesFolder != null)
{
string queryName = "All Workitems";
QueryHierarchyItem newBugsQuery = null;
if (myQueriesFolder.Children != null)
{
newBugsQuery = myQueriesFolder.Children.FirstOrDefault(qhi => qhi.Name.Equals(queryName));
}
WorkItemQueryResult result = witClient.QueryByIdAsync(newBugsQuery.Id).Result;
if (result.WorkItemRelations.Count() > 0)
{
int skip = 0;
const int batchSize = 100;
IEnumerable<WorkItemLink> workItemRefs;
do
{
workItemRefs = result.WorkItemRelations.Skip(skip).Take(batchSize);
if (workItemRefs.Count() > 0)
{
// get details for each work item in the batch
List<WorkItem> workItems = witClient.GetWorkItemsAsync(workItemRefs.Select(wir => wir.Target.Id)).Result;
foreach (WorkItem workItem in workItems)
{
// write work item to console
if ((string)workItem.Fields["System.WorkItemType"] == "Requirement")
{
Console.WriteLine("{0} {1}", workItem.Id, workItem.Fields["System.Title"]);
//workItem doesn't have properties for history or attachmens...
WorkItemHistory history = (WorkItemHistory) workItem.Fields["System.History"];
}
}
}
skip += batchSize;
}
while (workItemRefs.Count() == batchSize);
}
}
答案 0 :(得分:1)
为了检索历史记录,您需要遍历工作项的修订版(WorkItem.Revisions)并输出每个修订版的历史记录字段。
您不能使用WorkItem.Revision,而应使用WorkItem.Fields [“System.History”]。value来检索它。
附件在WorkItem.Attachments中,用于读取和写入。
答案 1 :(得分:0)
使用命名空间:
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.WorkItemTracking
有关详细信息,请参阅以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace TFSAPI
{
class Program
{
static void Main(string[] args)
{
string project = "https://xxxxxxxx.visualstudio.com/defaultcollection";
NetworkCredential nc = new NetworkCredential("username","pwd");
BasicAuthCredential cred = new BasicAuthCredential(nc);
TfsClientCredentials tc = new TfsClientCredentials(cred);
tc.AllowInteractive = false;
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(project), tc);
WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects["Sonar"];
WorkItemType workItemType = teamProject.WorkItemTypes["User Story"];
int workitemid = 2;
WorkItem wi = workItemStore.GetWorkItem(workitemid);
//Update history
wi.Fields["History"].Value = "You comments";
//Add attachment
wi.Attachments.Add(new Attachment("E:\\a\\1.txt"));
wi.Save();
}
}
}