关于 azure devops:如何在 Visual Studio Online 和 C# 中向工作项添加附件

How to add an attachment to a work item in Visual Studio Online and C#

我在从 http://www.visualstudio.com/en-us/integrate/reference/reference-vso-work-item-work 向 Visual Studio Online 中的工作项添加附件时遇到问题-items-vsi#UpdateworkitemsAddanattachment

我从我观看的收件箱中提取电子邮件,然后在 Visual Studio Online 中创建工作项。在这种情况下,我找到了对电子邮件的回复,并希望将历史记录添加到 vso 工作项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
    }
}

如果我这样做,它可以工作(显然没有添加附件):

1
2
3
PatchProperty[] properties = new PatchProperty[]{
    new PatchProperty("add","/fields/System.History", historyEntry.ToString()),
};

但有:

1
2
3
4
5
6
7
8
9
10
11
12
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 将是"超链接"而不是评论。