API to fetch all users chat messages in msteam

Casepoint Developer 56 Reputation points
2022-01-20T11:27:43.237+00:00

Hello,

I am trying to fetch all user's chats (Channel, Group, and one-to-one) messages. I am able to fetch all user's channels message with the Application scope, but not able to get all user's groups and one-to-one messages. I have tried this API https://graph.microsoft.com/v1.0/users/{user-id}/chats but it is giving the following response

{
"error": {
"code": "BadRequest",
"message": "List chats is only supported in delegated (on-behalf-of user) context.",
"innerError": {
"date": "2022-01-20T10:46:54",
"request-id": "d52403a5-e963-442c-af90-47d713b0f4da",
"client-request-id": "d52403a5-e963-442c-af90-47d713b0f4da"
}
}
}

How can I fetch all users chat with an Admin account?

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,016 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,485 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.
2,818 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Vasil Michev 94,366 Reputation points MVP
    2022-01-20T13:09:50.833+00:00

    As the documentation explains, in order to use this endpoint via application permissions, you need to request Microsoft to whitelist you app first: https://learn.microsoft.com/en-us/graph/teams-protected-apis


  2. JanardhanaVedham-MSFT 3,536 Reputation points
    2022-01-20T14:38:45.933+00:00

    Hi @Casepoint Developer ,

    Below Microsoft Graph teamwork APIs are available to get chat messages that a user is a participant in. Based on your requirement, you can consider using chats: getAllMessages Graph Teamwork API.

    1. chats: getAllMessages API : Get all messages from all chats that a user is a participant in, including one-on-one chats, group chats, and meeting chats. Currently this API is only supported with application permissions scope. GET https://graph.microsoft.com/v1.0/users/{id | user-principal-name}/chats/getAllMessages Note : Before you use this API with application permissions, you must request access for your app. For details, see Protected APIs in Microsoft Teams. Also , This API has licensing and payment requirements. It supports both model=A and model=B query parameters. If no model is specified, evaluation mode will be used. Please refer this documentation for more information on this. 2.List chats API : Retrieve the list of chats that the user is part of. Currently this API is only supported with delegated permissions scope. Please refer this documentation for more information on this. As per the error message that you shared above, it seems you are calling List chats API with application permisions. GET https://graph.microsoft.com/v1.0/users/{user-id | user-principal-name}/chats GET https://graph.microsoft.com/v1.0/me/chats GET https://graph.microsoft.com/v1.0/chats Note: Upon my testing, the above List chats API is returing the chat messages for Singed-in delegated user only but not for the other users.

    Hope this helps.

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


  3. Nivedipa-MSFT 2,806 Reputation points Microsoft Vendor
    2022-01-24T11:35:06.44+00:00

    There is no way to get all chats list of all users in an organization.
    You can get the list of chats that the user is part of.

     GET  https://graph.microsoft.com/v1.0/users/{user-id | user-principal-name}/chats  
    

    Reference doc:chats-getallmessages

    You can raise a user voice here: User Voice

    Thanks,
    Nivedipa


    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    0 comments No comments