skip to Main Content

I’m using the Microsoft Graph API to retrieve messages from my inbox via Graph Explorer. I aim to filter and order the messages based on several criteria, including whether they have attachments. However, when I include hasAttachments in the $orderby clause alongside external email filters, I encounter an InefficientFilter error.

API Requests and Behavior:

Ordering by hasAttachments Alone (Works):

GET `https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages$orderby=hasAttachments&$count=true`

Applying External Email Filters Without hasAttachments (Initially Caused Error, Resolved with Additional Filter):
Initial Request Causing Error: GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages$filter=contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@out.com')) AND not(contains(from/emailAddress/address,'@out.local'))&$orderby=receivedDateTime desc&$count=true

Error Response:

{
    "error": {
        "code": "InefficientFilter",
        "message": "The restriction or sort order is too complex for this operation."
    }
}

Resolved by Adding receivedDateTime Filter:

GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages?
    $filter=receivedDateTime ge 1900-01-01 AND contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@out.com')) AND not(contains(from/emailAddress/address,'@out.local'))&$orderby=receivedDateTime desc &$count=true

Behavior: This endpoint works correctly after adding the receivedDateTime ge 1900-01-01 filter.

Combining External Email Filters with hasAttachments in $orderby (Causes Error):

GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages?
    $filter=receivedDateTime ge 1900-01-01 AND contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@out.com')) AND not(contains(from/emailAddress/address,'@out.local'))&$orderby=hasAttachments &$count=true

Error Response :

{
    "error": {
        "code": "InefficientFilter",
        "message": "The restriction or sort order is too complex for this operation."
    }
}

Problem:

I want to order the messages by hasAttachments while applying external email filters to prioritize emails from specific senders and exclude others. However, combining these filters with the hasAttachments ordering results in the InefficientFilter error.

Question:

Is there a way to order Microsoft Graph API messages by hasAttachments alongside external email filters without encountering the InefficientFilter error? If not, what are the recommended workarounds to achieve similar functionality?

2

Answers


  1. No, it’s not possible to achieve this by one query.

    One of the workaround can be to make two queries. List messages without the attachments and then with the attachments.

    GET /v1.0/me/mailfolders/inbox/messages?$filter=contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@out.com')) AND not(contains(from/emailAddress/address,'@out.local')) and hasAttachments eq false
    
    GET /v1.0/me/mailfolders/inbox/messages?$filter=contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@out.com')) AND not(contains(from/emailAddress/address,'@out.local')) and hasAttachments eq true
    
    Login or Signup to reply.
  2. When combining $filter and $orderby in single Microsoft Graph API query, it’s essential to follow certain guidelines as mentioned in this MS Document.

    Initially, I too got same error when I ran below query without including hasAttachments property in $filter query:

    GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages?
    $filter=receivedDateTime ge 1900-01-01 AND contains(from/emailAddress/address,'@') AND not(contains(from/emailAddress/address,'@outlook.com')) AND not(contains(from/emailAddress/address,'@gmail.com'))&$orderby=hasAttachments &$count=true
    

    Response:

    enter image description here

    To resolve the error, ensure that properties used in $orderby also appear in the $filter and are listed in the same order.

    In my case, I used below modified query and got response with messages ordered by both hasAttachments and external email filters like this:

    GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages?
    $filter=hasAttachments eq true AND receivedDateTime ge 1900-01-01 AND 
    contains(from/emailAddress/address, '@') 
    AND not(contains(from/emailAddress/address,'@outlook.com')) 
    AND not(contains(from/emailAddress/address,'@gmail.com'))&
    $orderby=hasAttachments, receivedDateTime desc&
    $count=true
    

    Response:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search