microsoftgraph / msgraph-sdk-python

MIT License
325 stars 42 forks source link

Documentation for using Microsoft Search API using python SDK #759

Open adhadse opened 3 weeks ago

adhadse commented 3 weeks ago

I'm referring to this completely separate search endpoint of MS Graph: https://learn.microsoft.com/en-us/graph/search-concept-messages. I'm looking to query for users' email box.

I don't know how this SDK can enable me to do the above POST query. Since, the normal $search on user's mailbox don't provide me a $filter, I'm looking to explore this search endpoint.

On its page it mentions (right after Caution):

A search query can include filters that end users enter in the Search text box in Outlook.

I don't know how can the filter can be used?

shemogumbe commented 2 weeks ago

Hello @adhadse in search, have you considered using a regular get method with filters, such as https://graph.microsoft.com/v1.0/me/messages?$filter=importance eq 'high' and the snippets here:

from msgraph import GraphServiceClient
from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder

graph_client = GraphServiceClient(credentials, scopes)

query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
        filter = "importance eq 'high'",
)

request_configuration = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
query_parameters = query_params,
)

result = await graph_client.me.messages.get(request_configuration = request_configuration)

https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http

adhadse commented 2 weeks ago

Yes, I've tried this, but the results are pretty vague. The query is usually a number or a single word, but the resulting emails don't actually contains the query text. That's why I wanted to try out the other endpoint.

async def search_in_messages(
    self, query
) -> AsyncGenerator[MessageCollectionResponse, None]:
    """Search for a query in user's mailbox.

    By defaults result sorted by the date and time that the message was sent.

    Args:
        query: str, query to search for

    Returns:
        Generator object
    """
    query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
        select=[
            "sender",
            "toRecipients",
            "subject",
            "body",
            "conversationId",
            "hasAttachments",
            "attachments",
            "sentDateTime",
            "receivedDateTime",
        ],
        expand=["attachments"],  # fetch attachments as well
        # search=f"\"{query}\" OR \"attachment:{query}\"",
        search=f'"body:{query}" OR "subject:{query}" OR "attachment:{query}"',
        # can't use filter with search
    )

    request_configuration = RequestConfiguration(query_parameters=query_params)

    result = await self.client.me.messages.get(
        request_configuration=request_configuration
    )

    yield result

    while result is not None and result.odata_next_link is not None:
        result = await self.client.me.messages.with_url(
            result.odata_next_link
        ).get()
        yield result