May I Update or edit Microsoft Teams chat and Link using Microsoft Graph API?

Akbar Husain 5 Reputation points
2023-06-12T05:23:45.81+00:00

Below is the code which I am using for Updating or editing the text of the teams chat meeting but I am not able to Edit the text. Is there any other way to Update it? If yes then can you please share an example for the same.

Private Async Sub FetchMessage(sender As Object, e As EventArgs)

Try
Dim chatId As String = "" 'Chat ID
Dim messageId As String = "" 'Message ID
Dim accessToken As String = "" 'Access Token
Using client As HttpClient = New HttpClient()
            client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", accessToken)
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
            Dim endpoint As String = $"https://graph.microsoft.com/v1.0/chats/{chatId}/messages/{messageId}"
            Dim response As HttpResponseMessage = Await client.GetAsync(endpoint)

            If response.IsSuccessStatusCode Then
                Dim json As String = Await response.Content.ReadAsStringAsync()
                Dim message = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ObjResponse)(json)
                message.body.content = "Updated message content"
                '.Content = New StringContent(message.ToString(), System.Text.Encoding.UTF8, "application/json")
                Dim request As HttpRequestMessage = New HttpRequestMessage(New HttpMethod("PATCH"), endpoint) With {
                    .Content = New StringContent(message.ToString(), System.Text.Encoding.Default, "application/json")
                }
                response = Await client.SendAsync(request)

                If response.IsSuccessStatusCode Then
                    Console.WriteLine("Chat message updated successfully.")
                Else
                    Console.WriteLine($"Failed to update chat message. StatusCode: {response.StatusCode}")
                End If
            Else
                Console.WriteLine($"Failed to retrieve chat message. StatusCode: {response.StatusCode}")
            End If
        End Using
    Catch ex As Exception

    End Try
End Sub

Public Class ObjResponse
        Public id As String
        Public messageType As String
        Public subject As String
        Public chatId As String
        Public from
        Public body
    End Class

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
10,894 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,705 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
4,069 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.