Share via

Microsoft Graph API - Python implementation - Search emails with pagination

Valerio Bondi 0 Reputation points
2025-03-20T13:29:08.4066667+00:00

Hello,

I would like to build a Python script that searches through different Microsoft sources (emails, files and messages).

I'm stuck with this use case:

  • Search an email from a specific sender, in a specific folder, managing pagination.

So let's suppose we have this situation:

  • Folder: Deleted Items
  • From: ******@test.com
  • Top: 5
  • Total results: 10

So I would get 2 pages of 5 results each.

I have tried different approaches

  • query.post() method: with this I was able to specify sender, and manage pagination with size and from_ fields:
      request_body = QueryPostRequestBody()
      search_request = SearchRequest()
      
      # Set entity types to search
      search_request.entity_types = [EntityType.Message]
      
      # Create search query
      search_query = SearchQuery()
      search_query.query_string = 'from:******@test.com'
      
      # Add query to request and page size and number
      search_request.query = search_query
      search_request.size = 5
      search_request.from_ = 5
      
      # Add request to request body
      request_body.requests = [search_request]
      
      result = await self.graph_service_client.search.query.post(body=request_body)
    
    but I get results just from "inbox" folder, and I didn't find a solution with query.post() method to specify the deleted folder. chatGPT proposed me to include "in:deleteditems" plus "from:", but it doesn't work (according to documentation in: doesn't exist: https://learn.microsoft.com/en-us/graph/search-query-parameter?tabs=python)
  • messages.get() method: with this method, I can easily specify the folder where to look
      result = await (
          self.user_client.me.mail_folders
              .by_mail_folder_id('deleteditems')
              .messages.get(request_configuration=request_configuration)
      )
    
    but it seems not possible to manage pagination. If I define the query params like this:
      # Create search query request body
      query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
          select=['body', 'from', 'id', 'importance', 'isRead', 'hasAttachments', 'receivedDateTime', 'subject', 'toRecipients'],
          search="\"from:******@test.com\"",
          top=5,
          skip=5
      )
      
      # Execute the search
      request_configuration = RequestConfiguration(
          query_parameters=query_params,
      )
      
      result = await (
          self.user_client.me.mail_folders
              .by_mail_folder_id('deleteditems')
              .messages.get(request_configuration=request_configuration)
      )
    
    It says: SearchWithSkip The query parameter '$skip' is not supported with '$search'.
  • I also tried how to include the skiptoken according to https://learn.microsoft.com/en-us/graph/paging?tabs=python, but it seems it's not supported by MessagesRequestBuilderGetQueryParameters. I tried to edit manually the library class in order to include the skiptoken in the API URL, but it works just if I skip 1 element, with 2 or more elements I get this message:
    ErrorExecuteSearchStaleData Please reissue the query with rowOffset = 0. The specified rowoffset is '2', but the results are stale. The searchRequestkey is ESQCacheKey...tokenValue...

So what should be the correct approach in Python to get emails specifying folder, search query (in my case from, but also date ecc) and manage pagination?

Thanks in advance!

Microsoft Security | Microsoft Graph
0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.