Get Excel workbooks using Microsoft Graph and MSAL in an Outlook Add-in
Learn how to build a Microsoft Outlook Add-in that connects to Microsoft Graph, finds the first three workbooks stored in OneDrive for Business, fetches their filenames, and inserts the names into a new message compose form in Outlook.
Integrating data from online service providers increases the value and adoption of your add-ins. This code sample shows you how to connect your Outlook add-in to Microsoft Graph. Use this code sample to:
- Connect to Microsoft Graph from an Office Add-in.
- Use the MSAL .NET Library to implement the OAuth 2.0 authorization framework in an add-in.
- Use the OneDrive REST APIs from Microsoft Graph.
- Show a dialog using the Office UI namespace.
- Build an Add-in using ASP.NET MVC, MSAL 3.x.x for .NET, and Office.js.
- Outlook on all platforms
To run this code sample, the following are required.
Visual Studio 2019 or later.
SQL Server Express (If it is not automatically installed with recent versions of Visual Studio.)
An Office 365 account which you can get by joining the Office 365 Developer Program that includes a free 1 year subscription to Office 365.
At least three Excel workbooks stored on OneDrive for Business in your Office 365 subscription.
Optional, if you want to debug on the desktop instead of Outlook Online: Outlook for Windows, version 1809 or higher.
A Microsoft Azure Tenant. This add-in requires Azure Active Directiory (AD). Azure AD provides identity services that applications use for authentication and authorization. A trial subscription can be acquired here: Microsoft Azure.
|Outlook Add-in Microsoft Graph ASP.NET||Microsoft|
|1.0||July 8th, 2019||Initial release|
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Build and run the solution
Configure the solution
In Visual Studio, choose the Outlook-Add-in-Microsoft-Graph-ASPNETWeb project. In Properties, ensure SSL Enabled is True. Verify that the SSL URL uses the same domain name and port number as those listed in the next step.
Register your application using the Azure Management Portal. Log in with the identity of an administrator of your Office 365 tenancy to ensure that you are working in an Azure Active Directory that is associated with that tenancy. To learn how to register your application, see Register an application with the Microsoft Identity Platform. Use the following settings:
REDIRCT URI: https://localhost:44301/AzureADAuth/Authorize
SUPPORTED ACCOUNT TYPES: "Accounts in this organizational directory only"
IMPLICIT GRANT: Do not enable any Implicit Grant options
API PERMISSIONS (Delegated permissions, not Application permissions): Files.Read.All and User.Read
Note: After you register your application, copy the Application (client) ID and the Directory (tenant) ID on the Overview blade of the App Registration in the Azure Management Portal. When you create the client secret on the Certificates & secrets blade, copy it too.
- In web.config, use the values that you copied in the previous step. Set AAD:ClientID to your client id, set AAD:ClientSecret to your client secret, and set "AAD:O365TenantID" to your tenant ID.
Run the solution
Open the Visual Studio solution file.
Right-click Outlook-Add-in-Microsoft-Graph-ASPNET solution in Solution Explorer (not the project nodes), and then choose Set startup projects. Select the Multiple startup projects radio button. Make sure the project that ends with "Web" is listed first.
On the Build menu, select Clean Solution. When it finishes, open the Build menu again and select Build Solution.
In Solution Explorer, select the Outlook-Add-in-Microsoft-Graph-ASPNET project node (not the top solution node and not the project whose name ends in "Web").
In the Properties pane, open the Start Action drop down and choose whether to run the add-in in desktop Outlook or with Outlook on the web in one of the listed browsers. (Do not choose Internet Explorer. See Known Issues below for why.)
Press F5. The first time you do this, you will be prompted to specify the email and password of the user that you will use for debugging the add-in. Use the credentials of an admin for your O365 tenancy.
NOTE: The browser will open to the login page for Office on the web. (So, if this is the first time you have run the add-in, you will enter the username and password twice.)
The remaining steps depend on whether you are running the add-in in desktop Outlook or Outlook on the web.
Run the solution with Outlook on the web
Outlook for Web will open in a browser window. In Outlook, click New to create a new email message.
Below the compose form is a tool bar with buttons for Send, Discard, and other utilities. Depending on which Outlook on the web experience you are using, the icon for the add-in is either near the far right end of this tool bar or it is on the drop down down menu that opens when you click the ... button on this tool bar.
Click the icon to open the task pane add-in.
Use the add-in to add the names of the first three workbooks in the user's OneDrive account to the message. The pages and buttons of the add-in are self-explanatory.
Run the project with desktop Outlook
- Desktop Outlook will open. In Outlook, click New Email to create a new email message.
- On the Message ribbon of the Message form, there is a button labelled Open Add-in in a group called OneDrive Files. Click the button to open the add-in.
- Use the add-in to add the names of the first three workbooks in the user's OneDrive account to the message. The pages and buttons of the add-in are self-explanatory.
- The Fabric spinner control appears only briefly or not at all.
- If you are running in Internet Explorer, you will receive an error when you try to login that says you must put
https://outlook.office365.com) in the same security zone. But this error occurs even if you have done that.
Questions and comments
We'd love to get your feedback about the Get Excel workbooks using Microsoft Graph and MSAL in an Office Add-in 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. Ensure your questions are tagged with [office-js], [MicrosoftGraph] and [API].
Copyright (c) 2019 Microsoft Corporation. All rights reserved.