.netazuremicrosoft-graph-apimicrosoft-graph-sdks

Ordering Microsoft Graph API Messages by hasAttachments with External Email Filters Causes "InefficientFilter" Error


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?


Solution

  • Based on the rules mentioned in the documentation:

    So, if you want to order messages by hasAttachments, you need to specify hasAttachments at the beginning of the filter clause.

    How to specify hasAttachments in the filter to return both messages with and without the attachments? Simply use or

    ?$filter=(hasAttachments eq true OR hasAttachments eq false) AND ...
    

    The query:

    GET /v1.0/me/mailfolders/inbox/messages?
    $filter=(hasAttachments eq true OR hasAttachments eq false) 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
    

    `