page_type: sample description: This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from Office Add-ins. products:
This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from Office Add-ins.
To run the completed project in this folder, you need the following:
If you don't have a Microsoft account, there are a couple of options to get a free account:
Open a browser and navigate to the Microsoft Entra admin center. Login using a Work or School Account.
Select Applications in the left-hand navigation bar, then select App registrations.
Select New registration. On the Register an application page, set the values as follows.
Office Add-in Graph Sample
.Single-page application (SPA)
and set the value to https://localhost:3000/consent.html
.Select Register. On the Office Add-in Graph Sample page, copy the value of the Application (client) ID and save it, you will need it in the next step.
Select Certificates & secrets under Manage. Select the New client secret button. Enter a value in Description and select one of the options for Expires and select Add.
Copy the client secret value before you leave this page. You will need it in the next step.
[!IMPORTANT] This client secret is never shown again, so make sure you copy it now.
Select API permissions under Manage, then select Add a permission.
Select Microsoft Graph, then Delegated permissions.
Select the following permissions, then select Add permissions.
Update the app registration to support Office Add-in single sign-on (SSO).
Select Expose an API. In the Scopes defined by this API section, select Add a scope. When prompted to set an Application ID URI, set the value to api://localhost:3000/YOUR_APP_ID_HERE
, replacing YOUR_APP_ID_HERE
with the application ID. Choose Save and continue.
Fill in the fields as follows and select Add scope.
access_as_user
Access the app as the user
Allows Office Add-ins to call the app's web APIs as the current user.
Access the app as you
Allows Office Add-ins to call the app's web APIs as you.
In the Authorized client applications section, select Add a client application. Enter a client ID from the following list, enable the scope under Authorized scopes, and select Add application. Repeat this process for each of the client IDs in the list.
d3590ed6-52b3-4102-aeff-aad2292ab01c
(Microsoft Office)ea5a67f6-b6f3-4338-b240-c655ddc3cc8e
(Microsoft Office)57fb890c-0dab-4253-a5e0-7188c88b2bb4
(Office on the web)08e18876-6177-487e-b8b5-cf950c1e598c
(Office on the web)Run the following command to generate and install development certificates for your add-in.
npx office-addin-dev-certs install
If prompted for confirmation, confirm the actions. Once the command completes, you will see output similar to the following.
You now have trusted access to https://localhost.
Certificate: <path>\localhost.crt
Key: <path>\localhost.key
Copy the paths to localhost.crt and localhost.key, you'll need them in the next step.
NEW_GUID_HERE
with a new GUID, like b4fa03b8-1eb6-4e8b-a380-e0476be9e019
.YOUR_CLIENT_ID_HERE
with the application ID from your app registration.Rename the example.env
file to .env
.
Edit the .env
file and make the following changes.
YOUR_APP_ID_HERE
with the Application Id you got from the App Registration Portal.YOUR_CLIENT_SECRET_HERE
with the client secret you got from the App Registration Portal.PATH_TO_LOCALHOST.CRT
with the path to your localhost.crt file from the output of the npx office-addin-dev-certs install
command.PATH_TO_LOCALHOST.KEY
with the path to your localhost.key file from the output of the npx office-addin-dev-certs install
command.Rename the config.example.js
file to config.js
.
Edit the config.js
file and make the following changes.
YOUR_APP_ID_HERE
with the Application Id you got from the App Registration Portal.In your command-line interface (CLI), navigate to this directory and run the following command to install requirements.
yarn install
Run the following command in your CLI to start the application.
yarn start
In your browser, go to Office.com and sign in. Select Create in the left-hand toolbar, then select Workbook.
Select the Home tab, then select Add-ins.
Select More Add-ins, then select My Add-ins.
Select Upload My Add-in, then select Browse. Upload your manifest.xml file.
Select the Import Calendar button on the Home tab to open the task pane.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
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.