export messages

Glenn Maxwell 13,141 Reputation points
2024-10-20T21:29:07.15+00:00

Hi All,

I have a Teams channel, and I want to export all the channel messages to a text or CSV file. This is a one-time activity. I have created an Azure service principal (Azure App registration) and assigned Microsoft Graph delegated API permissions (ChannelMessage.Read.All). I logged into the Microsoft developer portal and tried to export the messages, but I am stuck at this point. Please guide me? 111111-2222-3333-4444-5678956789 is my unified group id.

i have logged to https://developer.microsoft.com/en-us/graph/graph-explorer and tried the below

https://graph.microsoft.com/v1.0/teams/111111-2222-3333-4444-5678956789/allChannels

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. Rohit Raghuwanshi - MSFT 625 Reputation points Microsoft External Staff
    2024-10-23T11:48:38.4066667+00:00

    Hello Glenn Maxwell,

    Thank you for reaching out, Microsoft!

    Use the following endpoint to list messages from a specific channel:

    GET https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages
    

    Replace {team-id} with your team ID and {channel-id} with the specific channel ID you want to export messages from.

    Please follow the below steps:
    Ensure Permissions:

    • Verify that your Azure App registration has the necessary permissions. You mentioned ChannelMessage.Read.All, which is correct for reading channel messages.

    Get Access Token:

    • You need an access token to authenticate your API requests. You can obtain this using the OAuth 2.0 authorization code flow. Here’s a quick guide on how to get an access token:
    • Go to the Microsoft identity platform and follow the steps to get an access token.

    List Channel Messages:

    • Use the following endpoint to list messages from a specific channel:
        GET https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages
      

    Export Messages:

    • To export the messages to a CSV file, here is sample PowerShell script:
        
        $teamId = "111111-2222-3333-4444-5678956789"
        $channelId = "your-channel-id"
        $accessToken = "your-access-token"
        
        $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:\path\to\your\file.csv" -NoTypeInformation
      

    Please refer below documents for more details:
    https://learn.microsoft.com/en-us/graph/api/resources/teams-api-overview?view=graph-rest-1.0
    https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http

    I hope this helps!

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


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.