Rebex Graph
.NET client library for MS Graph API (Exchange Online)
Download 30-day free trial Buy from $199More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Searching
On this page:
The sample code on this page assumes you have already connected and authenticated to Microsoft 365 (Exchange Online) server.
Searching basics
To search for messages in a mailbox matching specified criteria, use Search()
method.
It accepts variable number of search parameters and supports basic logical operators, which makes complex queries possible.
Tip: You can choose which message fields to retrieve to improve the performance of your application.
// create Graph client instance, connect, log in // ... // find top page of messages in 'Inbox' which arrived today GraphMessageCollection list = client.Search( GraphFolderId.Inbox, GraphMessageFields.Envelope, GraphMessageSearchParameter.Arrived(DateTime.Today)); // print some info about found messages foreach (GraphMessageInfo info in list) { Console.WriteLine("[{0}] {1}", info.ReceivedDate, info.Subject); }
Tip: See the table below for common search criteria or the complete list for more.
Tip: the Search()
method uses the $filter
parameter in the Graph API query.
If you need to specifically use the $search
parameter instead, see
GraphMessageSearchQuery.RawSearch.
From, subject, full-text and other criteria
Graph API offers many search parameters. The table below lists the most common ones. See the API documentation for the complete list of search criteria.
Search parameter | Description |
---|---|
From(address) | Messages that contain the specified string in their From field. |
Subject(queryTerm) | Messages that contain the specified string in their Subject field. |
Body(queryTerm) | Messages that contain the specified string in their body. |
HasFlag(status) | Messages that are marked with the specified flag. |
Arrived(on) | Messages that arrived on the specified date (disregarding time). |
Arrived(since, before) | Messages that arrived in the specified date/time interval. |
Sent(on) | Messages that were sent on the specified date (disregarding time). |
Sent(since, before) | Messages that were sent in the specified date/time interval. |
IsRead(value) | Messages that are read or unread depending on the value specified. |
More... | See the complete list. |
The following code uses several search criteria together. Messages which conform to all criteria are returned:
// create Graph client instance, connect, log in // ... // find top page of messages in 'Inbox' // from Bob // which arrived today // and contains "Order" in subject GraphMessageCollection list = client.Search( GraphFolderId.Inbox, GraphMessageSearchParameter.From("bob@example.org"), GraphMessageSearchParameter.Arrived(DateTime.Today), GraphMessageSearchParameter.Subject("Order"));
Logical operators - AND, OR, NOT
You can combine search criteria using And
, Or
and Not
logical operators.
Please note that Search()
method uses And
operator by default when
multiple criteria are specified.
// create Graph client instance, connect, log in // ... // find top page of messages in 'Inbox' from // Joe OR Bob, // AND whose subject contains "Order", // AND NOT arrived today GraphMessageCollection list = client.Search(GraphFolderId.Inbox, GraphMessageSearchParameter.Or( GraphMessageSearchParameter.From("joe@example.org"), GraphMessageSearchParameter.From("bob@example.org")), GraphMessageSearchParameter.Subject("Order"), GraphMessageSearchParameter.Not( GraphMessageSearchParameter.Arrived(DateTime.Today)));
Retrieving specific fields
By default, the Search()
method returns a default view (headers such as From, To, Subject or dates).
To retrieve more (or less) information, use the GraphMessageFields
argument.
The following code shows how to find specific messages and retrieve only their IDs:
// create Graph client instance, connect, log in // ... // find top page of messages in 'Inbox' which arrived today GraphMessageCollection list = client.Search( GraphFolderId.Inbox, GraphMessageFields.Id, GraphMessageSearchParameter.Arrived(DateTime.Today)); // store IDs and process them later // ... // ... download whole message using its ID when needed client.GetMessage(list[0].Id, stream);
Paging results
A folder can contain thousands of items. Retrieving all of them might consume a lot of bandwidth and take a very long time. Additionally, it's usually not even possible because Exchange server limits the maximum number of items it can return in response to a single API call.
For many applications, working with several hundreds of latest items is entirely sufficient.
When it isn't, use GraphPageView
.
The following code shows how to retrieve all messages in the 'Inbox' folder by calling
Search()
multiple times using the paging functionality:
// create Graph client instance, connect, log in // ... // find all messages in 'Inbox' from Bob int offset = 0; int pageSize = 1000; while (true) { // get next page GraphMessageCollection messages = client.Search( GraphFolderId.Inbox, new GraphPageView(offset, pageSize), GraphMessageSearchParameter.From("bob@example.org")); // print info about messages foreach (GraphMessageInfo m in messages) { Console.WriteLine("[{0}] ({1}) {2}", m.ReceivedDate, m.From, m.Subject); } // break if there are no more messages if (messages.Count == 0) break; // set next offset offset += messages.Count; } // final offset corresponds to total number of messages listed Console.WriteLine("Total messages: {0}", offset);
Back to feature list...