How to Add Inline Images in Azure DevOps Work Item Description or Comment Using WorkItemTrackingHttpClient?
Image by Domonique - hkhazo.biz.id

How to Add Inline Images in Azure DevOps Work Item Description or Comment Using WorkItemTrackingHttpClient?

Posted on

Welcome to this informative article, where we’ll explore the process of adding inline images in Azure DevOps work item descriptions or comments using the WorkItemTrackingHttpClient. By the end of this article, you’ll be well-equipped to embed images seamlessly into your work items, making them more engaging and informative for your team members and stakeholders.

Why Add Inline Images to Azure DevOps Work Items?

Before we dive into the technical aspects, let’s discuss the benefits of adding inline images to your Azure DevOps work items:

  • Enhanced visualization: Images help to break up text-heavy descriptions and make work items more visually appealing, allowing team members to quickly understand complex concepts or ideas.
  • Improved communication: Inline images facilitate better communication among team members, reducing misunderstandings and misinterpretations.
  • Faster understanding: Visual aids like images enable team members to grasp information quickly, saving time and increasing productivity.

Prerequisites

Before you begin, ensure you have the following:

  • Azure DevOps project with access to the WorkItemTrackingHttpClient API.
  • A valid Azure DevOps personal access token (PAT) with necessary permissions.
  • .NET Core or .NET Framework project with the Microsoft.TeamFoundation.WorkItemTracking.HttpClient NuGet package installed.

Step 1: Create a WorkItemTrackingHttpClient Instance

To interact with the Azure DevOps WorkItemTracking API, you need to create an instance of the WorkItemTrackingHttpClient:


using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Common;

// Create a new instance of VssConnection
VssConnection connection = new VssConnection(new Uri("https://dev.azure.com/{organization}"), new VssCredentials(true));

// Create a new instance of WorkItemTrackingHttpClient
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

Step 2: Prepare the Image for Upload

Before uploading the image, ensure it’s in a format compatible with Azure DevOps (e.g., PNG, JPEG, or GIF). You can use any .NET library to read and convert the image to a byte array:


using System.Drawing;

// Load the image from a file or resource
Bitmap image = new Bitmap("path/to/image.jpg");

// Convert the image to a byte array
using (MemoryStream ms = new MemoryStream())
{
    image.Save(ms, ImageFormat.Jpeg);
    byte[] imageData = ms.ToArray();
}

Step 3: Upload the Image to Azure DevOps

Use the WorkItemTrackingHttpClient to upload the image to Azure DevOps:


// Create a new attachment
Attachment attachment = new Attachment()
{
    FileName = "image.jpg",
    ContentType = "image/jpeg",
    Contents = imageData
};

// Upload the attachment to Azure DevOps
AttachmentReference attachmentReference = workItemTrackingClient.CreateAttachmentAsync(attachment).Result;

Step 4: Add the Inline Image to the Work Item Description or Comment

To add the uploaded image as an inline image in a work item description or comment, you’ll need to create a new html-formatted string:


string imageUrl = $"{attachmentReference.Uri}";
string htmlContent = $"<img src='{imageUrl}' alt='Inline Image' />";

You can then use the WorkItemTrackingHttpClient to update the work item description or add a new comment with the inline image:


// Update the work item description
WorkItem workItem = workItemTrackingClient.GetWorkItemAsync(id: 123).Result;
workItem.Description = htmlContent;
workItemTrackingClient.UpdateWorkItemAsync(workItem).Wait();

// Add a new comment with the inline image
Comment comment = new Comment()
{
    Text = htmlContent
};
workItemTrackingClient.AddCommentAsync(id: 123, comment: comment).Wait();

Putting it All Together

The following code snippet demonstrates the entire process of adding an inline image to an Azure DevOps work item description or comment using the WorkItemTrackingHttpClient:


using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Common;
using System.Drawing;

// Create a new instance of VssConnection
VssConnection connection = new VssConnection(new Uri("https://dev.azure.com/{organization}"), new VssCredentials(true));

// Create a new instance of WorkItemTrackingHttpClient
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

// Load the image from a file or resource
Bitmap image = new Bitmap("path/to/image.jpg");

// Convert the image to a byte array
using (MemoryStream ms = new MemoryStream())
{
    image.Save(ms, ImageFormat.Jpeg);
    byte[] imageData = ms.ToArray();
}

// Create a new attachment
Attachment attachment = new Attachment()
{
    FileName = "image.jpg",
    ContentType = "image/jpeg",
    Contents = imageData
};

// Upload the attachment to Azure DevOps
AttachmentReference attachmentReference = workItemTrackingClient.CreateAttachmentAsync(attachment).Result;

// Create a new html-formatted string for the inline image
string imageUrl = $"{attachmentReference.Uri}";
string htmlContent = $"<img src='{imageUrl}' alt='Inline Image' />";

// Update the work item description or add a new comment with the inline image
WorkItem workItem = workItemTrackingClient.GetWorkItemAsync(id: 123).Result;
workItem.Description = htmlContent;
workItemTrackingClient.UpdateWorkItemAsync(workItem).Wait();

// OR

Comment comment = new Comment()
{
    Text = htmlContent
};
workItemTrackingClient.AddCommentAsync(id: 123, comment: comment).Wait();

Conclusion

In this article, we’ve explored the step-by-step process of adding inline images to Azure DevOps work item descriptions or comments using the WorkItemTrackingHttpClient. By following these instructions, you’ll be able to enhance your work items with visual aids, improving communication and collaboration within your team.

Remember to adapt the code snippets to your specific use case, and don’t hesitate to ask if you have any questions or need further assistance.

Tip Description
Image Size Ensure the image size is reasonable to avoid affecting page load times.
Image Format Azure DevOps supports various image formats, including PNG, JPEG, and GIF.
Attachment Limits Azure DevOps has limits on attachment sizes (e.g., 4MB for work item attachments). Be mindful of these limits when uploading images.

We hope this article has been informative and helpful. Happy coding!

Frequently Asked Question

Get ready to unlock the secrets of adding inline images to Azure DevOps work item descriptions and comments using WorkItemTrackingHttpClient!

What is the recommended way to add an inline image to a work item description or comment using WorkItemTrackingHttpClient?

To add an inline image, you need to upload the image as an attachment to the work item, and then reference the attachment ID in the description or comment using the `![image description](attachment:)` syntax. Make sure to use the `WorkItemTrackingHttpClient` to upload the attachment and retrieve the attachment ID.

How do I upload an image as an attachment to a work item using WorkItemTrackingHttpClient?

Use the `WorkItemTrackingHttpClient` `AttachFileAsync` method to upload the image as an attachment to the work item. You’ll need to provide the work item ID, the file stream, and the file name. Make sure to capture the attachment ID returned in the response.

What is the correct syntax to reference the uploaded image attachment in the work item description or comment?

Use the `![image description](attachment:)` syntax, where `` is the ID of the uploaded image attachment. For example, `![My Image](attachment:123456)`. This will render the image inline in the work item description or comment.

Can I add multiple inline images to a work item description or comment using WorkItemTrackingHttpClient?

Yes, you can add multiple inline images by uploading each image as a separate attachment and referencing each attachment ID in the description or comment using the `![image description](attachment:)` syntax.

Are there any limitations or restrictions on adding inline images to work item descriptions or comments using WorkItemTrackingHttpClient?

Yes, there may be limitations on the size and type of images that can be uploaded as attachments, as well as restrictions on the number of attachments allowed per work item. Be sure to check the Azure DevOps documentation for the latest information on attachment limitations.

Leave a Reply

Your email address will not be published. Required fields are marked *