Microsoft Security | Microsoft Graph
An API that connects multiple Microsoft services, enabling data access and automation across platforms
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
So let's suppose we have this situation:
So I would get 2 pages of 5 results each.
I have tried different approaches
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) 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'.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!
An API that connects multiple Microsoft services, enabling data access and automation across platforms