Comments (16)
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.
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.
from aspnet-snippets-sample.
from aspnet-snippets-sample.
from aspnet-snippets-sample.
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.
from aspnet-snippets-sample.
from aspnet-snippets-sample.
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.
from aspnet-snippets-sample.
from aspnet-snippets-sample.
from aspnet-snippets-sample.
The binary contents should be in the attachment.ContentBytes property. It's a byte[], not a stream.
Re: side notes.
- There should be a Close issue button at the bottom of the page.
- Maybe you pasted the text from a formatted doc?
from aspnet-snippets-sample.
from aspnet-snippets-sample.
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.
from aspnet-snippets-sample.
Related Issues (20)
- Question: How to get the group picture along with group properties HOT 1
- How to set up open extension in application manifest under optionalclaims? HOT 1
- Microsoft Graph is only for Azure AD? HOT 1
- Image src attribute in email body using microsoft graph HOT 1
- Where is the Resources resource? HOT 1
- Authorization_RequestDenied . Insufficient privileges to complete the operation HOT 1
- Unable to update Group description
- An exception is thrown when a user's manager is requested, and is null. HOT 4
- MSAL.NET 3 - breaks these samples? HOT 2
- How to create SharePoint documentSet using onedrive api? HOT 1
- Microsoft.Identity.Web.MicrosoftGraphExtensions AddMicrosoftGraph extension HOT 5
- Creating class team
- Authentication Error - Value cannot be null. (Parameter 'value') HOT 3
- Microsoft.Identity.Web v1.6.0 (IServiceCollection doesn't have AddSignIn) HOT 1
- App crashes on File menu with personal account HOT 2
- Calendar not working HOT 3
- This repo is missing important files
- Missing Areas? HOT 1
- Access to Sharepoint sites
- Missing files
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnet-snippets-sample.