Microsoft Graph API Teams ChatMessageAttachment with jpeg image failing

Viewed 276

I've successfully posted a message to a Teams channel using the Micorosoft.Graph.GraphServiceClient (version 3.14). However I would like to include an image attachment (via ChatMessageAttachment). According to the documentation in order to do this I should set the ChatMessageAttachment.ContentType to "image/jpeg" and set the ContentUrl to a base64 string representation of the image file. Here is my code:

var chatMessage = new ChatMessage
{
    Subject = subject,
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = message // Including <attachment id="<imageId>"></attachment> 
    }        
};

chatMessage.Attachments = new List<ChatMessageAttachment>
{
    new ChatMessageAttachment
    {
        Id = imageId,
        ContentType = "image/jpeg",
        ContentUrl = base64String,
        Name = "HunnySuckle.jpg"
    }
};    

var res = await graphClient.Teams[teamId].Channels[channelId].Messages
    .Request()
    .AddAsync(chatMessage);

However when I post the request the response is 500 with the follwing body:

{
  "error": {
    "code": "InternalServerError",
    "message": "Failed to process request.",
    "innerError": {
      "date": "2020-09-18T20:39:46",
      "request-id": "44143c44-1c48-4a90-9bb5-20a3ba1b9905",
      "client-request-id": "44143c44-1c48-4a90-9bb5-20a3ba1b9905"
    }
  }
}

This is the extract from the docs (https://docs.microsoft.com/en-us/graph/api/resources/chatmessageattachment?view=graph-rest-1.0)

contentType . . image/: Image type with the type of the image specified ex: image/png, image/jpeg, image/gif. Populate the contentUrl field with the base64 encoding of the file in data: format.

However I'm not sure what "in data: format" means - maybe this could be the key to why it's not working.

The only other reference this error that I've found by searching online is on this page: https://github.com/microsoftgraph/microsoft-graph-docs/issues/7211 which is the same problem as mine I think (but sadly no answer).

0 Answers
Related