question

MeirRotfleisch-7325 avatar image
0 Votes"
MeirRotfleisch-7325 asked ShivamDhiman-1582 answered

C# Orderby and Filter to on Microsoft.Graph.GetMessages at the same time

HI
I am able to do a GetMessages with Orderby or with Filter but not with both at the same time

  var events = await graphClient.Me.Messages.Request()
             .Skip(p)
                
             .Filter("(from/emailAddress/address) eq 'someone@someplace.com'")
             .OrderBy("receivedDateTime desc")
             .GetAsync();


When i run this i get "The restriction or sort order is too complex for this operation."


suggestions


microsoft-graph-mail
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ShivamDhiman-1582 avatar image
1 Vote"
ShivamDhiman-1582 answered

Hi @MeirRotfleisch-7325

When using $filter and $orderby in the same query to get messages, make sure to specify properties in the following ways:

  • Properties that appear in $orderby must also appear in $filter.

  • Properties that appear in $orderby are in the same order as in $filter.

  • Properties that are present in $orderby appear in $filter before any properties that aren't.

Failing to do this results in the following error:

  • Error code: InefficientFilter

  • Error message: The restriction or sort order is too complex for this operation.

You can edit your code like this

 var messages = await graphClient.Me.Messages
     .Request()
     .Filter("receivedDateTime ge 2022-01-01T00:00:00Z and from/emailAddress/address eq 'o365mc@microsoft.com'")
     .OrderBy("receivedDateTime DESC")
     .GetAsync();

For more details you can refer to this documentation and similar post.

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.