1

I'm using the python MS graph sdk to try to perform a query. I'm able to successfully get all of the user's emails, but I can't seem to be able to use the sdk to get the emails from a certain time range. I've searched the their repo

https://github.com/microsoftgraph/msgraph-sdk-python

and also googled for quite a while and there doesn't seem to be any examples on how to do so.

Anyone have an idea of how to do it?

MAIL_FOLDER_ID = "inbox"
SELECT_FIELDS = [
    "id",
    "webLink",
    "from",
    "toRecipients",
    "receivedDateTime",
    "subject",
    "body",
    "lastModifiedDateTime",
    "hasAttachments",
    "attachments",
    "bodyPreview",
]
TOP_VALUE = 1000 
ORDER_BY = ["receivedDateTime DESC"]
SCOPES = ["https://graph.microsoft.com/.default"]

query_params = (
      MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
            select=SELECT_FIELDS, top=TOP_VALUE, orderby=ORDER_BY
      )
)
        
self.request_config = (
      MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
            query_parameters=query_params
      )
)

page = (
       await self.graphClient
             .me
             .mail_folders.by_mail_folder_id(MAIL_FOLDER_ID)
             .messages.get(request_configuration=self.request_config)
)

1 Answer 1

1

You can filter messages by createdDateTime, receivedDateTime and sentDateTime

query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
    filter = "receivedDateTime ge 2025-04-01T11:54:31Z and receivedDateTime le 2025-04-17T11:54:31Z",
    select = SELECT_FIELDS, orderby=ORDER_BY
)

Tricky part is when you use $orderby and $filter in the same query.

In that case 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.

Let's say you want to order messages by receivedDateTime, but filter messages by createdDateTime, so receivedDateTime must appear in $filter before any other properties.

query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
    filter = "receivedDateTime ge 1900-01-01 and createdDateTime ge 2025-04-01T11:54:31Z and createdDateTime le 2025-04-02T11:54:31Z",
    select = SELECT_FIELDS, orderby=ORDER_BY
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.