Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs
Hi Kiran Varkala,
Thank you for posting your question in the Microsoft Q&A forum.
I understand you're looking to retrieve the latest message from a specific Teams channel based on a timestamp field. The chatMessage resource includes several timestamp properties such as createdDateTime, lastModifiedDateTime, deletedDateTime, and lastEditedDateTime. Based on your phrasing, I assume you're specifically interested in sorting by createdDateTime.
After researching, here are a few suggestions and insights:
- The standard API
GET /teams/{team-id}/channels/{channel-id}/messagesdoes not support$orderby=createdDateTime. Messages are returned based onlastModifiedDateTime, and the only supported query options are$topand$expand. To get the latest message by creation time, you’ll need to fetch a batch (e.g., using$top=50) and sort them manually on the client side. Once you identify the message you're interested in, you can use the endpointGET /teams/{team-id}/channels/{channel-id}/messages/{message-id}to retrieve its full details.
- For a more dedicated and real-time approach, consider using the Change Notifications API. You can subscribe to message events (create/update/delete) in a channel and receive push notifications when new messages are posted. This avoids the need for polling and gives you immediate access to the latest content.
Reference:
- https://learn.microsoft.com/en-us/graph/api/channel-list-messages?view=graph-rest-1.0&tabs=http
- https://learn.microsoft.com/en-us/graph/api/chatmessage-get?view=graph-rest-1.0&tabs=http
- https://learn.microsoft.com/en-us/graph/teams-changenotifications-chatmessage
I hope these resources help!
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.