【问题标题】:Create an Issue (WorkItem) in Azure DevOps with Attachments using a REST API (C#)使用 REST API (C#) 在带有附件的 Azure DevOps 中创建问题 (WorkItem)
【发布时间】:2020-11-30 16:33:45
【问题描述】:

我需要在DevOps中创建一个带有附件的Issue类型的WorkItem,今天我正在创建但没有附件,我想知道如何通过json添加附件并进行POST,我的代码如下:了解,令牌和验证在别处)

代码是用 C# 编写的:

string requestUrl = "https://dev.azure.com/"+organizacao+"/"+project+"/_apis/wit/workitems/$Chamado Issue?api-version=6.0"; //Verificar sempre a versão da api          
                    
            
            //Montando o json/Atribuindo valores aos campos
            try
            {
             //   if (ExisteDevOps()=="0")
               //{  //Criando Lista de campos
                    List<Object> Fields = new List<Object> { };

                    //Titulo
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/System.Title",
                        value = this.Titulo
                    });

                    //Descricao
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/System.Description",
                        value = this.Descricao
                    });
                    //Nome de usuario
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/Custom.a0cfd3d4-4ce1-4426-b360-261f00ea6a3b",
                        value = this.Nome_Usuario
                    });
                    //Email
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/Custom.1b77b0ed-15c3-4e9f-8a66-ac83b9375d65",
                        value = this.email
                    });
                    //SetorSolcitante
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/Custom.SetorSolicitante",
                        value = this.setor
                    });
                    //Sistema
                    Fields.Add(new
                    {
                        op = "add",
                        path = "/fields/Custom.Sistema",
                        value = this.sistema
                    });
                    

                  

              

                                   

                

【问题讨论】:

    标签: c# azure-devops devops


    【解决方案1】:

    查看下面的代码sn-p,这是一个.net示例:

    [ClientSampleMethod]
    public WorkItem AddAttachment()
    {
        int id = Convert.ToInt32(Context.GetValue<WorkItem>("$newWorkItem3").Id);
        string filePath = ClientSampleHelpers.GetSampleTextFile();
    
        VssConnection connection = Context.Connection;
        WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
    
        // upload attachment to store and get a reference to that file
        AttachmentReference attachmentReference = workItemTrackingClient.CreateAttachmentAsync(filePath).Result;
    
        JsonPatchDocument patchDocument = new JsonPatchDocument();
    
        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Test,
                Path = "/rev",
                Value = "1"
            }
        );
    
        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/System.History",
                Value = "Adding the necessary spec"
            }
        );
    
        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/-",
                Value = new
                {
                    rel = "AttachedFile",
                    url = attachmentReference.Url,
                    attributes = new { comment = "VanDelay Industries - Spec" }
                }
            }
        );
    
        WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id).Result;
    
        return result;
    }
    

    您可以通过以下链接获取更多示例:

    https://github.com/microsoft/azure-devops-dotnet-samples/blob/main/ClientLibrary/Samples/WorkItemTracking/WorkItemsSample.cs

    如果你想使用 REST api,你可以参考Attachments - Create api:

    POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?fileName={fileName}&uploadType={uploadType}&areaPath={areaPath}&api-version=6.0
    

    【讨论】:

    • 谢谢,我的错误是在添加到 json 之前,我需要将其上传到 azure,从而获得我将在问题中发布的链接
    • 你的意思是你的问题已经解决了吗?如果我的回复对您有帮助,您可以Accept it as an Answer,这对阅读此主题的其他社区成员会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2020-07-12
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    相关资源
    最近更新 更多