How MS Graph API works when $filter and $top query params used together

Hasit Parmar 0 Reputation points
2024-04-26T06:08:23.5433333+00:00

I am using MS Graph API for fetch group chats of users with below details:

Endpoint: /users/<user-id>/chats?filter=chatType+eq+%27group%27&$top=1

Here ideal expectation is that, it shoul filter all the chats first and then return first record, but i was experiencing reverse behavior that it select top n records first and then apply given filter.

Is this valid behavior? Our main motive is to get recently updated group chat of particular user.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,662 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yakun Huang-MSFT 235 Reputation points Microsoft Vendor
    2024-04-26T08:35:43.46+00:00

    Hi @Hasit Parmar

    Your need is to get the recently updated group chats of a specific user, so first of all, you need to filter with $filter, make sure that the type you get is a group, and then you need to sort the obtained group chats in descending order according to the update time. so that the first place is the most recent group chat.

    The $top parameter is used for pagination, and the value of top is used to indicate the number of articles displayed on each page, obviously it can't be used for sorting, so we need to use $orderby parameter sorts the update time in descending order.

    The request code is as follows:

    https://graph.microsoft.com/v1.0/users/{user-id}/chats?$orderby=lastMessagePreview/createdDateTime desc&$filter=chatType eq 'group'
    

    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