unable to export messages

Roger Roger 7,326 Reputation points
2024-11-01T17:20:46.0966667+00:00

Hi All,

I have a Teams channel, and I want to export all the channel messages to a CSV file. I have created an Azure App registration and assigned Microsoft Graph API Application permission (ChannelMessage.Read.All).

Using Postman, I have generated the access token. With the script below, I am able to generate a CSV file that includes the message subject but not the message body. Additionally, if the subject is blank, I do not get any output.

I want to export messages that include both the subject and message body. If the subject is empty, I want to export the message body . If the subject is present but there is no message body, I want to export the subject as well. please guide me.

$TeamId = "Teams-ID"
$ChannelId = "Channel-ID"
$accessToken = "tokengenerated"
$headers = @{
    "Authorization" = "Bearer $accessToken"
  }
  $messages = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/messages" -Headers $headers
  $messages.value | Export-Csv -Path "C:\output.csv" -NoTypeInformation

Exchange Online
Exchange Online
A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.
Microsoft Teams | Development
Microsoft Teams | Development
Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs
Microsoft Security | Microsoft Graph
Microsoft Teams | Microsoft Teams for business | Other
{count} votes

Answer accepted by question author
  1. CarlZhao-MSFT 46,411 Reputation points
    2024-11-04T09:16:46.3066667+00:00

    Hi @Roger Roger

    I just used your script locally to export a .csv file of the message set. Even though the subject is empty, it still outputs fine.

    User's image

    So, I suggest you output the $messages variable in a PowerShell context and see if it returns your message set fine. If the issue occurs when outputting the $messages variable in a PowerShell context, then you may need to do some judgment on the message set returned. If it outputs fine in a PowerShell context, then it may be that some data was lost when converting to a .csv file.

    $TeamId = "Teams-ID" 
    $ChannelId = "Channel-ID" 
    $accessToken = "tokengenerated"
    
    $headers = @{
        "Authorization" = "Bearer $accessToken"
    }
    
    # Retrieve messages
    $messages = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$TeamId/channels/$ChannelId/messages" -Headers $headers
    
    # Create an empty array to store messages
    $messageList = @()
    
    # Iterate through each message
    foreach ($message in $messages.value) {
        $subject = $message.subject
        $body = $message.body.content
        $id = $message.id
        $from = $message.from.user.displayName
        $createdDateTime = $message.createdDateTime
       
     # If the subject is empty but there is a body, or there is a subject, but the body is empty, or both exist
        if ($subject -or $body) {
            $messageList += [PSCustomObject]@{
                Id = $id
                From = $from
                CreatedDateTime = $createdDateTime
                Subject = $subject
                Body = $body
            }
        }
    }
    
    # Export to CSV file
    $messageList | Export-Csv -Path "C:\output.csv" -NoTypeInformation
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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