0 votes
by (260 points)

In the GraphClient, I would like to get the output ordered based on the retrieval date. I have been looking at both GetMessageListAsync() and SearchAsync() and neither of them give me the result I want. They both return the number of records I want and I can order them myself but there is no guarantee that they will give you the most recent messages, they could give you older ones.

According to what I found online, this should be possible using pure odata queries where I could do an orderby. Something like

GET /me/mailFolders('inbox')/messages?$orderby=receivedDateTime%20desc&$top=10

The concept I need this for is mailboxes which have an Inbox folder well over 100k messages. To avoid having to load them all, I would like to load the messages received in the last 10 days or the first 2500 (whatever limit I hit first)

Applies to: Rebex Secure Mail

1 Answer

+1 vote
by (75.3k points)

Currently, the GraphClient class does not have high-level API for sorting. However, you can specify it in native format using GraphMessageSearchQuery.RawOrderBy property like this:

using (var client = new GraphClient())
{
    client.Connect();
    client.Login(token);
    client.LogWriter = new ConsoleLogWriter(LogLevel.Debug);

    var query = new GraphMessageSearchQuery()
    {
        RawOrderBy = "receivedDateTime desc",
        PageView = new GraphPageView(0, 10)
    };

    var list = client.Search(GraphFolderId.Inbox, query);
    // process messages
}

If you check the produced log, the request URL corresponds to the one you asked for:

Sending request: GET /v1.0/me/mailFolders/inbox/messages?$orderby=receivedDateTime%20desc&$top=10
...