If you are looking for the sample from Ignite 2017 that used the Bing Speech API and the List API together, the URL posted was incorrect. That sample code is available here: https://github.com/OneDrive/document-library-audio-transcription-robot-sample
This project provides an example implementation for connecting Azure Functions to OneDrive to enable your solution to react to changes in files in OneDrive nearly instantly.
The project consists of two parts:
- An Azure Function definition that handles the processing of webhook notifications and the resulting work from those notifications
- An ASP.NET MVC application that activates and deactivates the OneDrive Data Robot for a signed in user.
In this scenario, the benefit of using Azure Function is that the load is required by the data robot is dynamic and hard to predict. Instead of scaling out an entire web application to handle the load, Azure Functions can scale dynamically based on the load required at any given time. This provides a cost-savings measure for hosting the application while still ensuring high performance results.
To get started with the sample, you need to complete the following steps:
- Register a new application with Azure Active Directory, generate an app password, and provide a redirect URI for the application.
- Create a new Azure Function and upload the code files in the AzureFunction folder into the function's definition.
- Run the sample project and sign-in with your Office 365 account and activate the data robot by clicking the Activate button.
- Navigate to OneDrive and modify a file (see below for details).
- Watch the data robot update the file automatically.
To register a new application with Azure Active Directory, log into the Azure Portal.
After logging into the Azure Portal, follow these steps to register the sample application:
- Navigate to the Azure Active Directory module.
- Select App registrations and click New application registration.
- Type the name of your file handler application.
- Ensure Application Type is set to Web app / API
- Enter a sign-on URL for your application, for this sample use
https://localhost:44382
. - Click Create to create the app.
- After the app has been created successfully, select the app from the list of applications. It should be at the bottom of the list.
- Copy the Application ID for the app you registered and paste it into two places:
- In the
Web.config
file on the line:<add key="ida:ClientId" value="[ClientId]" />
- In the
run.csx
file on the line:private const string idaClientId = "[ClientId]";
- In the
- Configure the application settings for this sample:
- Select Reply URLs and ensure that
https://localhost:44382
is listed. - Select Required Permissions and then Add.
- Select Select an API and then choose Microsoft Graph and click Select.
- Find the permission Have full access to user files and check the box next to it, then click Select, and then Done.
- Select Keys and generate a new application key by entering a description for the key, selecting a duration, and then click Save. Copy the value of the displayed key since it will only be displayed once. Paste it into two places:
- In the
Web.config
file on the line:<add key="ida:ClientSecret" value="[ClientSecret]" />
- In the
run.csx
file on the line:private const string idaClientSecret = "[ClientSecret]"
- In the
- Select Reply URLs and ensure that
To create the Azure Function portion of this sample, you will need to be logged into the Azure Portal.
- Click New and select Function App in the Azure Portal.
- Enter a name for your function app, such as datarobot99. The name of your function app must be unique, so you'll need to modify the name to find a unique one.
- Select your existing Azure subscription, desired resource group, hosting plan, and location for the Azure Function app.
- Choose to create a new storage for this azure function, and provide a unique name for the storage.
- Click create to have the Azure Portal create everything for you.
After the required components have been provisioned, click on the App Services module in the portal and find the Azure Function App we just created.
To create a new Azure Function application and setup a function for this project:
- Click the
+
next to Functions. - Select the Webhook + API scenario, choose CSharp as the language, and then Create this function.
- Copy the code from
run.csx
and paste it into the code editor and then click Save. - On the right side, select View files to expand the files that make up this function.
- Click Upload and then navigate to the
project.json
file in the AzureFunction folder and upload it. This file configures the dependencies for the Azure Function, and will add the Azure authentication library and the Microsoft Graph SDK to the function project. - Click Integrate on the left side, under the HttpTriggerCSharp1 function name (or if your function has a different name, select Integrate under that). Configure your function accordingly:
- Select HTTP (req) under Triggers and configure the values accordingly then click Save.
- Allowed HTTP methods: Selected methods
- Mode: Standard
- Request parameter name: req
- Route template: default value (empty)
- Authorization level: Anonymous
- Selected HTTP methods: Uncheck everything except POST
- Select New Input on the Inputs column and choose Azure Table Storage. Set the parameters accordingly:
- Table parameter name: syncStateTable
- Table name: syncState
- Storage account connection: Click new and then connect it to the storage connection you created (named something like datarobot99ae20).
- Leave the other parameters with their default values, and then click Save.
- Select New Input again, and again choose Azure Table Storage. Set the parameters accordingly:
- Table parameter name: tokenCacheTable
- Table name: tokenCache
- Storage account connection: Choose the existing storage connection from step 2, something like datarobot99ae20_STORAGE.
- Leave the other parameters with their default values, and then click Save.
- Click back on the function name in the left navigation column to bring up the code editor.
- Click Get function URL and copy the URL for this function.
- Paste this value into the
Web.config
file on the line:<add key="ida:NotificationUrl" value="[azureFunctionServiceUrl]" />
- Paste this value into the
- Navigate to the Azure Portal and go to Storage Accounts on the left-hand navigation bar.
- Click on the storage account created in step 6.2 (e.g., datarobot99ae20).
- Click on Access keys (under Settings).
- Copy one of the Connection String values under Default keys.
- Paste this value into the
Web.config
file on the line:<add key="StorageConnectionString" value="[azureFunctionStorageConnectionString]" />
.
Now that everything is properly configured, open the web project in Visual Studio and press F5 launch the project in the debugger.
- Sign in to the data robot project and authorize the application to have access to the data in your OneDrive.
- After you authorize the data robot, you should see a Subscription ID and Expiration date/time. These values are returned from the Microsoft Graph webhook notification subscription that powers the data robot. By default the expiration time is 3 days from when the robot is activated.
If no value is returned, check to ensure that the notification URL is correct in the Web.config
file, and verify in the Azure Function console that you are seeing a request successfully processed by your function code.
This sample data robot uses a Web API to insert live stock quotes into Excel files while you are editing them. To invoke the data robot and ask it to insert a stock quote into a cell, you can do the following:
- Open your OneDrive ({tenant}.onedrive.com).
- Click New then Excel Workbook. A new Excel workbook will open in the Excel web application.
- The data robot looks for the keyword
!odbot
followed by what you are asking for:- To retrieve a stock quote, use
!odbot MSFT stock quote
where MSFT can be replaced with the stock ticker symbol of your choice.
- To retrieve a stock quote, use
If the data robot is unable to retrieve a real stock quote, it will make one up so the demo always works.
Note: OneDrive webhooks can take up to 5 minutes to be delivered, depending on load and other conditions. As a result, requests in the workbook may take a few minutes to be updated with real data.
For more information about Microsoft Graph API, see Microsoft Graph.
See License for the license agreement convering this sample code.