page_type | urlFragment | products | languages | description | extensions | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
office-add-in-auth-graph-react |
|
|
Learn how to build a Microsoft Office Add-in, as a single-page application with no backend, that connects to Microsoft Graph, finds the first three workbooks stored in OneDrive for Business, fetches their filenames, and inserts the names into an Office document using Office.js. |
|
Learn how to build a Microsoft Office Add-in, as a single-page application (SPA) with no backend, that connects to Microsoft Graph, finds the first three workbooks stored in OneDrive for Business, fetches their filenames, and inserts the names into an Office document using Office.js.
Integrating data from online service providers increases the value and adoption of your add-ins. This code sample shows you how to connect your SPA add-in to Microsoft Graph. Use this code sample to:
- Connect to Microsoft Graph from an Office Add-in.
- Use the MSAL.js Library to implement the OAuth 2.0 authorization framework in an add-in, using the Auth Code Flow w/ PKCE for SPAs.
- Use the OneDrive REST APIs from Microsoft Graph.
- Show a dialog using the Office UI namespace.
- Build an Add-in using React, MSAL.js, and Office.js.
- Use add-in commands in an add-in.
- Excel on Windows (one-time purchase and subscription)
To run this code sample, the following are required.
-
Node and npm, version 18.20.2 or later (npm version 10.5.0 or later).
-
TypeScript version 5.4.3 or later.
-
A Microsoft 365 account. You can get one if you qualify for a Microsoft 365 E5 developer subscription through the Microsoft 365 Developer Program; for details, see the FAQ. Alternatively, you can sign up for a 1-month free trial or purchase a Microsoft 365 plan.
-
At least three Excel workbooks stored on OneDrive for Business in your Office 365 subscription.
-
Office on Windows, version 16.0.6769.2001 or higher.
-
A Microsoft Azure Tenant. This add-in requires Azure Active Directory (AD). Azure AD provides identity services that applications use for authentication and authorization. A trial subscription can be acquired here: Microsoft Azure.
-
A code editor.
Solution | Author(s) |
---|---|
Office Add-in Microsoft Graph React | Microsoft |
Version | Date | Comments |
---|---|---|
1.1 | December 10th, 2020 | Upgrade MSAL.js to v2 |
1.0 | August 29th, 2019 | Initial release |
1.1 | January 14th, 2021 | Changed system for creating and installing the SSL certificates for HTTPS. |
1.2 | April 4th, 2024 | Updated to MSAL 3.7.1. Refactored code. |
-
Navigate to the Azure portal - App registrations page to register your app.
-
Sign in with the admin credentials to your Microsoft 365 tenancy. For example, [email protected].
-
Select New registration. On the Register an application page, set the values as follows.
- Set Name to
ExcelGraphDemo
. - Set Supported account types to Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox).
- In the Redirect URI section, ensure that Single-page application (SPA) is selected in the drop down and then set the URI to
https://localhost:3000/login/login.html
. - Select Register.
For more information on how to register your application, see Register an application with the Microsoft Identity Platform.
Note: The sample uses the OAuth 2.0 Auth Code Flow w/ PKCE for SPAs, which requires no secrets.
- Set Name to
-
On the ExcelGraphDemo page, copy and save the value for the Application (client) ID. You'll use it in the next section.
-
In a code editor, open the
/login/login.ts
file in the project. Near the top is a configuration property calledclientId
. Replace theYOUR APP ID HERE
placeholder value with the application ID you copied in the previous step. Save and close the file. -
Open the
/logout/logout.ts
file in the project. Near the top is a configuration property calledclientId
. Replace theYOUR APP ID HERE
placeholder value with the application ID you copied in the previous step. Save and close the file. -
Open a Command Prompt as an administrator.
-
Navigate to the root of the sample, which would normally be
[PATH-TO-YOUR-PROJECTS]\Office-Add-in-samples\Samples\auth\Office-Add-in-Microsoft-Graph-React
. -
Run the command
npm install
. -
Run the command:
npx office-addin-dev-certs install --machine
.If you get the following prompt, click Yes.
Note: If you have worked with another Office Add-in within the last 30 days that was originally created with the Yo Office tool, you may have unexpired certs for localhost already, in which case you will get a message saying that localhost is already trusted. If so, continue with the next section.
-
In the command prompt, run the command
start npm start
. This will open a second command prompt, build the project and then start a server (with dev mode settings). It takes from 5 to 30 seconds. When it finishes, the last line should sayCompiled successfully
. Minimize this command prompt. -
Back in the original command prompt, run the command
npm run sideload
. This will launch Excel and install the add-in in it. After a few seconds, a OneDrive Files group appears on the right end of the Home ribbon with a button named Open Add-in. -
Click the Open Add-in to open the task pane add-in.
-
The pages and buttons in the add-in are self-explanatory.
Note: The first time that you press the Connect to Office 365 button and sign in, you will be prompted to consent to the add-in.
- When a dialog is opened (with either the Connect to Office 365 or the Sign out from Office 365 buttons) on a Windows computer, a process named Desktop App Web Viewer starts on the computer. (You can see it in Task Manager.) These processes don't always close when the dialog closes. If you are working with the sample a lot, opening and closing dialogs, these processes use more and more memory. Eventually, the login dialog will start to flicker and seem to reload itself over and over. If this happens, use Task Manager to kill the processes.
- Did you experience any problems with the sample? Create an issue and we'll help you out.
- We'd love to get your feedback about this sample. Go to our Office samples survey to give feedback and suggest improvements.
- For general questions about developing Office Add-ins, go to Microsoft Q&A using the office-js-dev tag.
Copyright (c) 2019 and 2021 Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
Note: The Index.html file contains an image URL that tracks diagnostic data for this sample add-in. Please remove the image tag if you reuse this sample in your own code project.