简体   繁体   中英

Create an Issue (WorkItem) in Azure DevOps with Attachments using a REST API (C#)

I need to create a WorkItem of Issue type with attachments in DevOps, today I am creating but without attachment, I would like to know how I add the attachments through json and do the POST, my code I do as follows: understood, tokens and validations are elsewhere)

The code was made in 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
                    });
                    

                  

              

                                   

                

Check the code snippet below, this is a .net sample:

[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;
}

You could get more samples in the the following link:

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

If you want to use REST api, you could refer to Attachments - Create api:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM