我在向 Visual Studio Online 中的工作项添加附件时遇到问题,从 http://www.visualstudio.com/en-us/integrate/reference/reference-vso-work-item-work-items-vsi#UpdateworkitemsAddanattachment 开始工作。
我从我查看的收件箱中提取电子邮件,然后在 Visual Studio Online 中创建工作项。在本例中,我找到了一封电子邮件的回复,并希望将历史记录添加到 vso 工作项中:
public static bool AppendHistoryToTicket(int ticketID, Message mailMessage)
{
StringBuilder historyEntry = new StringBuilder(1024);
historyEntry.AppendLine(mailMessage.Sender.Address + " replied on " + mailMessage.DateTimeCreated.ToString("dd MMM yyyy HH:mm:ss") + " GMT - ");
historyEntry.AppendLine("");
historyEntry.AppendLine(mailMessage.Body.Content);
var uri = TicketURI.UpdateWorkItem(ticketID);
WebRequest request = WebRequest.CreateHttp(uri);
request.ContentType = "application/json-patch+json; charset=utf-8";
request.Method = "PATCH";
request.Headers.Add("Authorization", Settings.UserAuthorization);
PatchProperty[] properties = new PatchProperty[]{
new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
new PatchProperty("add", "/relations/-", new
{
rel = "AttachedFile",
url = mailMessage.Attachment.URL,
attributes = new
{
comment = "This is a test"
}
})
};
byte[] byte1 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(properties));
request.ContentLength = byte1.Length;
using (Stream stream = request.GetRequestStream())
stream.Write(byte1, 0, byte1.Length);
Logger.Write("Updating the history of ticket: #" + ticketID);
using (WebResponse response = request.GetResponse())
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
JObject jsonObj = JObject.Parse(sr.ReadToEnd());
Models.Ticket result = new Models.Ticket(JsonConvert.DeserializeObject<DTOs.Ticket>(jsonObj.ToString()));
return result != null;
}
}
如果我这样做,它就会起作用(显然无需添加附件):
PatchProperty[] properties = new PatchProperty[]{
new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
};
但是:
PatchProperty[] properties = new PatchProperty[]{
new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
new PatchProperty("add", "/relations/-", new
{
rel = "AttachedFile",
url = mailMessage.Attachment.URL,
attributes = new
{
comment = "This is a test"
}
})
};
我收到 400(错误请求)
如有任何帮助,我们将不胜感激
最佳答案
我相信您想要的是“添加超链接”调用,而不是从同一 MSDN 页面进行调用,因为您只想添加链接 (mailMessage.Attachment.URL) 而不是上传文档。这意味着 rel 将改为“超链接”并且没有评论。
关于attachment - 如何在 Visual Studio Online 和 C# 中向工作项添加附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404540/