Search code examples
.netemail-attachmentsmicrosoft-graph-sdks

How do I list emails with attachment names using Microsoft Graph SDK?


I can retrieve the emails and attachments using this code:

var emails = await graphServiceClient.Users["user@company.com"].MailFolders["Inbox"].Messages
            .Request()
            .Top(10)
            .Expand("attachments")
            .Select("subject,receivedDateTime,from,attachments")
            .GetAsync(cancellationToken);

However I'm not interested in the contents for the attachments (expanding the attachments is around 3x slower). Is it possible to get just the attachment names in a single call? If I leave out the .Expand then the Attachments property is null.


Solution

  • Try to use $select in Expand to return only selected properties for expanded property.

    var emails = await graphServiceClient.Users["user@company.com"].MailFolders["Inbox"].Messages
                .Request()
                .Top(10)
                .Expand("attachments($select=name)")
                .Select("subject,receivedDateTime,from,attachments")
                .GetAsync(cancellationToken);