Brent Arias
Brent Arias

Reputation: 30165

Get Outlook EmailFolders and Emails with Graph SDK

I want to download all e-mails (and file attachments) within a folder hierarchy that matches a glob pattern. For example, I could grab all e-mails Messages (with attachments) in folders that match the pattern "inbox/jira/mentions" or perhaps "inbox/*/jira". Furthermore, I might also filter the attachments based on globbing their filenames as well, such as "*.docx".

The holy grail answer would cause the server to perform the glob matching on the folders as well as the attachment names. However, I suspect I will need to do some of the globbing locally.

I don't know if the OData syntax directly supports any of that. If it does, how do I leverage it using the SDK provided in the Microsoft.Graph SDK? It would be ideal if both Messages and Attachments were already "expanded" as part of that query.

If the SDK does not support that, then conceptually I think I might do the following: Perform an EmailFolder query only, and convert the results into stringified folder paths. Stash this as a "lookup table" for locally performed glob matching. Any matching folders are then used as a query to fetch relevant Messages (with expanded Attachments). As for filtering attachments based on filename, I have no idea for how that looks.

I don't have code examples for any of the above. What I do have is an example Messages request which illustrates SDK usage:

messages = await graphClient.Users[user.UserPrincipalName].Messages.Request()
    .Filter("HasAttachments eq true")
    .Expand("Attachments")
    .Select(m => new
    {
        m.Id,
        m.From,
        m.ToRecipients,
        m.Subject,
        m.HasAttachments,
        m.CreatedDateTime,
        m.LastModifiedDateTime,
        m.ParentFolderId,
        m.Body
    })
    .Top(pageSize).GetAsync(cancellationToken);

Sample code, using this SDK, is what I'm asking for. I need to expand my understanding of what is possible with this SDK and its query capabilities.

The sample code above raises a number of questions. Unlike the above, if I start my request root with MailFolders, what is the syntax to request an "Expand" of both Messages and Attachments simultaneously? In other words, two-levels of depth with the Expand function. Otherwise, can the above Messages request be modified to give the "display name" of the parent folder, instead of just the ID of the parent folder?
If yes, is that related to the AdditionalData property? Is there a way to request just the names of the file attachments, WITHOUT also downloading those attachments? Is there a way to specify a glob pattern filter for the attachment filenames?

All of this relates to my original stated intention. Again, code samples would be perfect.

Upvotes: 1

Views: 1281

Answers (1)

Shiva Keshav Varma
Shiva Keshav Varma

Reputation: 3575

You cannot do 2 level expand as it is already specified in this document. To get the displayName of the parent folder you need to use

GET https://graph.microsoft.com/v1.0/me/mailFolders/{Parentid} 

You can find basic samples in the documentation. Here is a sample application where we can start with and integrate messages, people and other API's.

You can also use Graph Explorer for testing the calls and also you can see code snippets tab and grab SDK code according to your language.

Upvotes: 1

Related Questions