Users can now share a OneDrive item directly from within an Outlook add-in. In this sample, we show you how to use the JavaScript API for Office, and the OneDrive API to create a Microsoft Outlook Add-in that displays which email recipients have permission to view the OneDrive link in the message body. If there are recipients that don't have the proper permission to view the link(s), the user will have the option to grant permissions to selected recipients.
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.
- Prerequisites
- 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. You can sign up for an Office 365 Developer subscription and get an Office 365 account through it.
- 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.
- 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.js
file, 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.
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.
app.js
- In app.js, a global object of recipients is created by using theOffice.context.mail.item.getAsync
to get recipients from the message. Links are obtained in the same way, withOffice.context.mail.item.body.getAsync
.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.
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.