Microsoft Outlook Add-in Sharing to OneDrive
With the OneDrive
shares API, you can programmatically get permissions for an item by using the item's link. You can then use the same
shares API, with
action.invite, to share the URL with email recipients.
Table of Contents
- Configure the project
- Run the project
- Understand the code
- Questions and comments
- Additional resources
This sample requires the following:
- Visual Studio 2015. If you don't have Visual Studio 2015, you can install Visual Studio Community 2015 for free.
- Microsoft Office Developer Tools for Visual Studio 2015.
- Microsoft Office Developer Tools Preview for Visual Studio 2015. Note that both base and preview of Microsoft Office Developer Tools for Visual Studio 2015 must be installed.
- Outlook 2016.
- A computer running Exchange with at least one email account, or an Office 365 account. If you don't have either, you can join the Office 365 Developer Program and get a free 1 year subscription to Office 365.
- A personal OneDrive account. This is different from an Exchange account.
- Internet Explorer 9 or later, which must be installed, but doesn't have to be the default browser. To support Office Add-ins, the Office client that acts as host uses browser components that are part of Internet Explorer 9 or later.
Note: This sample currently only works with consumer OneDrive service.
Configure the project
- Get a token from the OneDrive developer site. To get a token, go to OneDrive authentication and sign in and click Get Token. Copy the token, which is after the text Authentication: bearer and save it to a text file. This token is valid for one hour, and gives you read/write access to the signed in user's OneDrive files. You'll be required to sign in to your personal OneDrive.
- Open the solution file OutlookAddinOneDriveSharing.sln and in the
\app\authentication.config.jsfile, paste the token, like this:
TOKEN = '<your_token>';
In Solution Explorer, click the OutlookAddinOneDriveSharing project and in the Properties window, change Start Action to Office Desktop Client.
Right-click the OutlookAddinOneDriveSharing project and choose Set as StartUp Project.
- Close Outlook desktop client.
Run the project
Press F5 to run the project. You'll be prompted to enter an email and password to use for running Outlook. Enter your Exchange email and password. Note This may be different from your personal OneDrive account email and password.
Once the Outlook desktop client has started, click New Email to compose a new message.
Important If you weren't prompted to accept the installation for the IIS Express Development Certificate, navigate to Control Panel | Add/Remove Programs and choose IIS Express. Right-click and choose Repair. Restart Visual Studio and open the OutlookAddinOneDriveSharing.sln file.
This add-in uses add-in commands, so you launch the add-in by choosing this command button on the ribbon:
A task pane appears with the list of recipients. The list is divided by who has permission to view the link, and who doesn't. Note Any time you add or remove recipients, or change the link, click the command button again to refresh the list.
To get a OneDrive link, sign in to your OneDrive account at www.onedrive.com and choose one of your files. Copy the link for that file and paste it into the body of the email message.
Understand the code
app.js- In app.js, a global object of recipients is created by using the
Office.context.mail.item.getAsyncto get recipients from the message. Links are obtained in the same way, with
onedrive.share.service.js- An object to handle requests to the OneDrive API. This object includes:
- A link property to maintain links.
- A request method to send requests to the OneDrive API endpoint, and to use the shares and permissions API.
- A UI object to render the display to the task pane.
render.controller.js- An object to control the display in the task pane.
- The sample checks only the first link in the message body.
- You must use a personal OneDrive account to get the token.
- If you are using an Outlook account for your personal OneDrive account and it hasn't been migrated to Office 365, sharing may not work. To check if your email account was migrated, sign in to Outlook.com and if the upper left hand corner says Outlook.com, it's not migrated.
Questions and comments
We'd love to get your feedback about the Outlook Add-in Sharing to OneDrive sample. You can send your feedback to us in the Issues section of this repository. Questions about Office 365 development in general should be posted to Stack Overflow. Make sure that your questions are tagged with [Office365] and [API].
- Office 365 APIs documentation
- Microsoft Office 365 API Tools
- Office Dev Center
- Office 365 APIs starter projects and code samples
- OneDrive developer center
- Outlook developer center
Copyright (c) 2016 Microsoft. All rights reserved.