![](/img/trans.png)
[英]How to attach a file to work item in TFS without physical file path?
[英]File attachment to a work item - TFS 2015
我正在嘗試使用TFS測試用例工作項附加文件。 第一步,我試圖在TFS附件存儲中創建附件文件。 一旦在附件存儲中創建了文件,我將得到AttachmentReference對象,並使用該對象,將文件附加到具有ID 595的所選工作項。但是,我的過程掛在CreateAttachmentAsync函數調用上。 任何幫助表示贊賞!
public void AttachFile(VssConnection connection)
{
//use the workItemTrackingHttpClient
try
{
using (WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>())
{
//create a work item
//WorkItem wi = witClient.GetWorkItemAsync(595, expand: WorkItemExpand.All).Result;
string filePath = @"C:\Temp\attach-file.PNG";
AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result;
JsonPatchDocument patchDocument = new JsonPatchDocument();
//add fields to your patch document
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachRef.Url,
attributes = new { comment = "Adding new attachment for Test Case 2" }
}
}
);
WorkItem result = witClient.UpdateWorkItemAsync(patchDocument, 595).Result;
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
嘗試刪除AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result
。 請嘗試以下代碼:
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using System;
namespace UploadAttachment
{
class Program
{
static void Main(string[] args)
{
// Full path to the binary file to upload as an attachment
string filePath = @"C:\Temp\attach-file.PNG";
var myCredentials = new VssClientCredentials();
var connection = new VssConnection(new Uri(@"http://tfsserver:8080/tfs/TeamProjectCollectionName"), myCredentials);
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
AttachmentReference attachment = workItemTrackingClient.CreateAttachmentAsync(filePath).Result;
JsonPatchDocument patchDocument = new JsonPatchDocument();
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.Url,
attributes = new { comment = "Adding new attachment for Test Case" }
}
}
);
WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, 595).Result;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.