GithubHelp home page GithubHelp logo

Comments (16)

DianeD avatar DianeD commented on July 21, 2024

Hi Tim. This sample uses the Graph SDK, so the equivalent request is this:

IMailFolderMessagesCollectionPage messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().Select("webLink,subject,hasAttachments").GetAsync();

You can use Expand to retrieve attachments in the same request:

IMailFolderMessagesCollectionPage messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().Select("webLink,subject,hasAttachments").Expand("attachments").GetAsync();

What is it that you're trying to get into the Razor page? Are you extending this sample app to show attachment data?

from aspnet-snippets-sample.

DianeD avatar DianeD commented on July 21, 2024

Sorry, I didn't see your additional info until I refreshed the page. The sample isn't currently designed to show binary results. To indicate successful file download, it displays some stream info, like in this example that shows the stream size:

        public async Task<ActionResult> GetMyInboxMessagesThatHaveAttachments()
        {
            ResultsViewModel results = new ResultsViewModel();
            List<ResultsItem> items = new List<ResultsItem>();

            try
            {

                // Initialize the GraphServiceClient.
                GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

                // Get messages in the Inbox folder.
                IMailFolderMessagesCollectionPage messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().Filter("hasAttachments eq true").Expand("attachments").GetAsync();
                
                if (messages?.Count > 0)
                {
                    foreach (Message message in messages)
                    {
                        items.Add(new ResultsItem
                        {
                            Display = message.Subject,
                            Id = message.Id,
                            Properties = new Dictionary<string, object>
                            {
                                { "Attachment count", message.Attachments.Count },
                                { "First attachment name", message.Attachments[0].Name },
                                { "First attachment type", message.Attachments[0].ODataType },
                                { "First attachment size", message.Attachments[0].Size }
                            }
                        });
                    }
                }
                results.Items = items;
            }
            catch (ServiceException se)
            {
                if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();

                // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
                return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
            }
            return View("Test", results);
        }

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

DianeD avatar DianeD commented on July 21, 2024

Hi Tim. The attachment name and size should be on the items in the attachments collection, for example: message.Attachments[0].Name

I'm getting the stream too, but the ContentLocation property is null. This sounds like what you're looking for. I'll try to find out how to get it.

contentLocation: The Uniform Resource Identifier (URI) that corresponds to the location of the content of the attachment.

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

DianeD avatar DianeD commented on July 21, 2024

Hi Tim. No news on ContentLocation yet.

I did confirm that it's by design that hasAttachments does not apply to inline attachments. I also learned this: If the message has inline attachments, they're indicated in the HTML of the message's content property with a src attribute like: src=\"cid:[email protected]\

To avoid a single expensive request, you could send requests to the messages/{id}/attachments endpoint (like in your first comment) as needed. Then you could select the attachment properties you want.

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

DianeD avatar DianeD commented on July 21, 2024

The binary contents should be in the attachment.ContentBytes property. It's a byte[], not a stream.

image

Re: side notes.

  1. There should be a Close issue button at the bottom of the page.
  2. Maybe you pasted the text from a formatted doc?

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

DianeD avatar DianeD commented on July 21, 2024

Hi Tim. I could see the ContentBytes property in the local variable, but I had to cast it as a FileAttachment in order to access it. For example:

IMessageAttachmentsCollectionPage attachments = await graphClient.Me.Messages[message.Id].Attachments.Request().GetAsync();
foreach (Attachment attachment in attachments)
{
    if (attachment.ODataType == "#microsoft.graph.fileAttachment")
    {
        FileAttachment fileAttachment = attachment as FileAttachment;
        byte[] contentBytes = fileAttachment.ContentBytes;
    }
}

Note I'm querying the Attachments endpoint directly, which differs from your previous example.

I added the check for attachment type because only attachments of type FileAttachment have the ContentBytes property. This includes FileAttachments that are inline.

I'll check whether this is expected behavior (the explicit cast).

from aspnet-snippets-sample.

twcarnahan avatar twcarnahan commented on July 21, 2024

from aspnet-snippets-sample.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.