attachment - 如何在 Visual Studio Online 和 C# 中向工作项添加附件

标签 attachment azure-devops

我在向 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/

相关文章:

java - 在 GWT 中使用 RequestBuilder 处理附件以响应

java - 如何通过java代码将任何文件作为附件附加到JIRA问题?

database - 将图像存储到 Access 数据库的附件字段中

python - 如何跳过处理作为另一封电子邮件附件的电子邮件的附件

将库部署到 Databricks DBFS 403 禁止错误的 Azure DevOps CD 管道

azure - Azure YAML Pipelines <部署作业> 可以使用可变环境吗?

java - 如何以编程方式访问 GMAIL 附件?

azure-devops - 在多个其他管道全部完成后触发 Azure Pipeline 构建

api - Azure DevOps API : Get all files (source and target) of Pull Request

python - 如何通过 python 以编程方式提取 Azure IP 范围 json 文件?