Search code examples
office365outlook-addinoutlook-restapioutlook-web-addins

Outlook Web Addin, retrieving attachments


I'm trying to figure out how to retrieve the attachments from an e-mail. I already have a working web add-in that accesses several of the attributes of the mail, such as subject, recipients, sender, body etc.

Now I want to retrieve the attachments as well and to figure out how this is done, I downloaded the sample project https://github.com/OfficeDev/Outlook-Add-in-Javascript-GetAttachments.

I cannot make it work though. Under the section Build and debug it says to set the project named AttachmentExampleService to have Action = start. This seems to be incorrect though, as this project is the rest service in the solution and not the project that drives the interface - I did try it though but it just makes the project unusable because it obviously doesn't launch not browser nor the Outlook client all depending on which is set to be the client for the project.

If I set nothing to Action = start (which is default) the correct web application starts when I run the solution from VS2017 and the expected button also appears both in the browser and in the Outlook client.

However, regardless of it being the browser or the Outlook client (2016 in this case) the add-in won't work properly.

A button named "Test Attachments" appears as expected, but pressing it always produces the same result which is "Unknown error There was an unexpected error: 0 -- ".

I can see in the code (Home.js in the AttachmentExampleWeb project) that the "0" is the status property of the XMLHttpRequest object and after the "--" was supposed to be the statusText property of the XMLHttpRequest object, which in this case is empty.

I have also tried to make the rest service write some output to a file, but nothing happens at all in this regard - I don't think the while process ever gets this far.

I have also tried to set the right ports for the projects (there is a port mentioned in the url for the ajax call in javascript file) but it seens to make no difference.

I have also tried to have Fiddler running to sniff what's being sent, but I can't see anything there that I can understand and determine is an error in this case.

Does anyone have an idea what the problem could be here? Or is there a better/newer code sample somewhere out there I just haven't found?

All help and/or pointers appreciated :)


Solution

  • Ok, so addressing what I think is your real question here, "how do I access attachments in an add-in?"

    The Outlook add-in API doesn't give direct access to attachment contents. It gives you metadata about the attachments (name, size, id), but not the bits themselves. You have to use that metadata to retrieve the contents of the file using either EWS or REST. Typically since files can be large, and downloading them can take some time, add-ins don't do the downloading from within the add-ins' JS code, instead, they push the metadata to a backend service which handles the task.

    The sample the Outlook team referred to uses this approach. If you're not interested in the writing to OneDrive stuff (which is the reason for the Graph client and the SSO component to the sample), then yes, SaveAttachmentsWithDistinctTokens is the place to look. You'll notice it uses the REST token retrieved by the add-in (via getCallbackTokenAsync) to get the attachment data via the Outlook REST API. Then you can do whatever you want with the result :)