Forum Discussion

RaviKiranS's avatar
RaviKiranS
Copper Contributor
May 04, 2023

How to add a comment with rich text (text+images) to the work item (task) with Powershell

Hi All,

I am trying to add a comment into discussion and other custom fields based of rich text programmatically (using PowerShell). Can you please guide me how to upload the image. I browsed online and able to send the text using REST API calls and AZ CLI cmdlets but how to upload images. Can you please provide some docs. or example reference. Thanks in advance!

2 Replies

  • Ankido88's avatar
    Ankido88
    Copper Contributor

    Hi

    prerequiaties token (PAT) win work items read and write and project and teams read scops.

    API v 7.1

     

    # Configuration
    $organization = "your-org"          # e.g., "contoso"
    $project = "your-project"           # e.g., "Fabrikam-Fiber-Git"
    $workItemId = 123                   # Work item ID
    $pat = "your-pat"                   # Personal Access Token
    $imagePath = "C:\path\to\image.png" # Path to image
    
    # Base64 encode PAT for auth
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
    
    # Step 1: Upload image as attachment
    $uploadUrl = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$(Split-Path $imagePath -Leaf)&api-version=7.1"
    $imageBytes = [System.IO.File]::ReadAllBytes($imagePath)
    $headers = @{
        Authorization = "Basic $base64AuthInfo"
        'Content-Type' = 'application/octet-stream'
    }
    
    try {
        $uploadResponse = Invoke-RestMethod -Uri $uploadUrl -Method Post -Body $imageBytes -Headers $headers
        $attachmentUrl = $uploadResponse.url
        Write-Host "Image uploaded: $attachmentUrl"
    } catch {
        Write-Error "Upload failed: $($_.Exception.Message)"
        exit
    }
    
    # Step 2: Create rich text with image (Markdown)
    $text = "Issue description. See image: ![Image]($attachmentUrl)"
    
    # Step 3: Add comment to work item
    $commentUrl = "https://dev.azure.com/$organization/$project/_apis/wit/workItems/$workItemId/comments?api-version=7.1-preview.3"
    $body = @{ text = $text } | ConvertTo-Json
    $headers['Content-Type'] = 'application/json'
    
    try {
        $commentResponse = Invoke-RestMethod -Uri $commentUrl -Method Post -Body $body -Headers $headers
        Write-Host "Comment added: $($commentResponse.commentId)"
    } catch {
        Write-Error "Comment failed: $($_.Exception.Message)"
    }

     

  • Take this:

     

    $org = "https://dev.azure.com/yourOrg"
    $project = "yourProject"
    $workItemId = 1234
    $pat = "yourPersonalAccessToken"
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
        "Content-Type" = "application/json"
    }
    
    $imageUrl = "https://yourdomain.com/path-to-image.jpg"
    $commentText = "<p>This is a rich text comment with an image:</p><img src='$imageUrl' alt='Image' width='300' />"
    
    $body = @{
        text = $commentText
    } | ConvertTo-Json -Depth 3
    
    Invoke-RestMethod -Uri "$org/$project/_apis/wit/workItems/$workItemId/comments?api-version=7.0" `
        -Method Post -Headers $headers -Body $body

     

Resources