This project provides a sample implementation of a file handler 2.0 for Microsoft Office 365.
The file handler allows creating, editing, and viewing Markdown text files with the md
file extension.
This sample also illustrates a custom action, with the Add to ZIP action added to all files.
To get started with the sample, you need to complete the following actions:
- Register a new application with Azure Active Directory, generate an app password, and provide a redirect URI for the application.
- Register the file handler extensions as an add-in for your application in Azure Active Directory.
- Run the sample project and sign-in with your Office 365 account and accept the consent prompt so the file handler is registered for your account.
- Navigate to OneDrive for Business or a SharePoint document library and allow file handlers to load.
- Click New, and then Markdown Document, to create a new file.
To register a new application with Azure Active Directory, log into the Azure Portal. File handler apps cannot be registered through the Application Registration Portal since that portal created converged applications which are not currently compatible with file handlers.
After logging into the Azure Portal, the following steps will allow you to register your file handler 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:44362
. - 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 the Web.config file on the line:
<add key="ida:ClientId" value="application id here" />
- Make a note of the Object ID for this application, since you will need this later to register the file handler manifest.
- Configure the application settings for this sample:
- Select Reply URLs and ensure that
https://localhost:44362
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 all files user can access 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 the value of the key you generated into the Web.config file in this project, inside the value for the line:
<add key="ida:ClientSecret" value="put application key here" />
- Paste the value of the key you generated into the Web.config file in this project, inside the value for the line:
- Select Reply URLs and ensure that
After registering your app with Azure Active Directory, you can upload the file handler manifest information into the application.
For detailed instructions on how to upload the file handler manifest, see Registering file handlers.
The file handler manifest for the sample file is available in the addin-schema-debug.json
file in this project.
The sample uses an Azure Storage table to cache user and token information. To successfully run the sample, you must provide an Azure Storage connection string in Web.config:
<add key="StorageConnectionString" value="[AzureStorageConnectionString]" />
To create a new storage instance and copy the connection string:
- Open the Azure Portal in your browser. You must have an Azure Subscription or trial subscription.
- Select Storage Accounts and then click Add.
- Name the storage account, leaving the defaults as they are. You will also need to create a new resource group and provide a name.
- Click Create to provision the storage account.
- After the storage account has finished provisioning, select it from the list of storage accounts. Click on the Access Keys tab, and copy the connection string for key1.
- Paste this connection string where
[AzureStorageConnectionString]
appears in Web.config.
Once your project is registered and configured, you're ready to run it. Press F5 to launch the project in the debugger. The file handler project will load in your default browser and be ready for you to #. # to the file handler project, and authorize the application to have access to the data in your OneDrive.
Once you have authorized the file handler to have access, the file handler will be available in OneDrive and SharePoint. After signing in to the app, click the "Try it in OneDrive" button to launch your OneDrive. Due to service caches, it may take a few minutes before your file handler shows up in OneDrive. You may need to close your browser and open it again before the file handler will be activated.
Now that the file handler is wired up, try creating a new Markdown document.
Click New and then Markdown Document.
OneDrive will create a new file with the .md
extension and launch the file handler app's editor.
You can add content to the file, click Save and then close the file handler's tab.
Now that you've created a markdown file in your OneDrive, click back to OneDrive and refresh the page.
Find the new file you created (look for MD File.md
) and click on it.
This will launch the file handler in preview mode.
You will see a preview of the content written into the Markdown file displayed inline in the OneDrive preview experience.
Next try to open the file using the file handler experience. Click the Open button, and select Open in {app name} to launch your file handler project and edit the file again. You can use the Rename button to rename the file in OneDrive or create a sharing link for the file by clicking Get Link.
The last part of this sample is to use the custom action provided by the file handler.
Close the file handler tab and switch back to OneDrive.
Select a file (click the check) and then in the toolbar, click ...
and then Add to ZIP - Debug.
This will launch the file handler's custom action handler and provide all of the items selected.
The file handler code will download the items, compress them into a ZIP file, and upload the file back to OneDrive as archive.zip
in the same folder.
For more information about file handlers, check out the OneDrive developer portal.
For more information about Microsoft Graph API, see Microsoft Graph.
See License for the license agreement covering this sample code.