skip to Main Content

I’ve a question, I automate so many tasks using the CLI tool

az boards work-item relation show
az boards work-item show
az boards work-item update

But now, my doubt is, as part of my automation, I need to upload an attachment to some workitems, is possible do that using the CLI?

Searching I found this link:

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?view=azure-devops-rest-7.0&tabs=HTTP#upload-a-text-file

But I do not see where specify the workitem ID where you want to upload the file, somebody face by this need?

Thank you, best regards.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you very much, it's works, your guide it's perfect and very clear. Just last question. I see that when generate the content, it's convert to oneline text, the result of multiline file is this:

    "Hello worldn                    another linen                    another linen"
    

    Note the start and finish character " and the addition of character n, this is my code to add the upload.

    systemlog = '''Hello world  
                        another line'''
    
    azure_api_header_data = {"cache-control": "no-cache", "Content-Type": "application/json", "Authorization": f"Basic {self.pat_token}"}
        
    upload_api_url = f"{self.organization}/{self.project}/_apis/wit/attachments?fileName={self.attachment_name}&uploadType=Simple&api-version=7.0"
    
    res_azupload = requests.post(url=upload_api_url, timeout=20, headers=azure_api_header_data, json=systemlog)
    

    By other side I tried to upload a binary method:

    https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?tabs=HTTP#upload-a-binary-file

    And when upload the result is this:

    enter image description here

    How can I do to keep the format and not be treated as string all the content?

    Thank you again.


  2. But I do not see where specify the work item ID where you want to upload the file, somebody face by this need?

    Yes, you can upload the attachment in a specific work item in two steps-

    First Create the attachment file using below URL

    POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?fileName=textAsFileAttachment.txt&api-version=7.0
    

    Here Request Body is "User text content to upload"

    enter image description here

    Second add this attachment to a specific work item using below URL

    PATCH https://{instance}/{collection}/{project}/_apis/wit/workitems/{id}?api-version=5.0
    

    Request Body-

    Use the above generated URL in below request body

    [
    {
    "op": "add",
    "path": "/relations/-",
    "value": {
    "rel": "AttachedFile",
    "url": "https://dev.azure.com/{collection}/yyyyyyyy/_apis/wit/attachments/xxxxxxxxxxx?fileName=textAsFileAttachment.txt",
    "attributes": {
    "comment": "Spec for the work"
    }
    }
    }
    ]
    

    enter image description here

    enter image description here

    In this way, you can upload an attachment to a work item.

    You can refer to this SO-Thread and MS docs as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search