The OneDrive & OneDrive for Business file picker is a light JavaScript picker over the OneDrive REST API. It is designed to work with both OneDrive & OneDrive for Business and to have light dependencies. Check your live demo.
Download the latest release here or build it from master.
You also need jQuery in your page. The file picker was built with jQuery 1.8.0, it should work with lower version.
Then include jQuery and file picker scripts in your page :
<script type="text/javascript" src="https://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="onedrive-file-picker.js"></script>
<link rel="stylesheet" type="text/css" href="onedrive-file-picker.css">
You can follow the OneDrive documentation page to obtain an access token. This part has to be done by your application, as the file picker needs an access token to work.
For OneDrive just init the file picker with access token :
var accessToken = '...';
var picker = new OneDriveFilePicker({ 'accessToken': accessToken });
For OneDrive for Business, the file picker needs an access token and the resource url to access the api.
var accessToken = '...';
var baseURL = 'https://{tenant}-my.sharepoint.com/_api/v2.0'; // For example : https://nuxeo-my.sharepoint.com/_api/v2.0
var picker = new OneDriveFilePicker({ 'accessToken': accessToken, 'baseURL': baseURL });
Once you have your file picker instance, you can call select method to open it. The method return a Promise, if you're not familiar with it, see this page. Once user has selected a file or closed the picker, the Promise will be resolved.
picker.select().then(function(result) {
// Your code here
});
You get an object with at least action attribute which could be :
- select - When user has selected a file
- close - When user has closed the file picker without selecting a file
In select case, you also get an
itemattribute which contains the selected item metadata.
You can also catch error thrown from API, like unauthorized errors, with catch Promise method :
picker.select().catch(function(error) {
// Your code here
});
The API error contains the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
See our contribution documentation.
Install Node.js and then use npm to install all the required libraries:
$ git clone https://github.com/nuxeo/onedrive-file-picker
$ cd onedrive-file-picker
$ npm install
You can run a live demo during development with hot reload, just enter in your terminal :
$ gulp demo
Nuxeo dramatically improves how content-based applications are built, managed and deployed, making customers more agile, innovative and successful. Nuxeo provides a next generation, enterprise ready platform for building traditional and cutting-edge content oriented applications. Combining a powerful application development environment with SaaS-based tools and a modular architecture, the Nuxeo Platform and Products provide clear business value to some of the most recognizable brands including Verizon, Electronic Arts, Sharp, FICO, the U.S. Navy, and Boeing. Nuxeo is headquartered in New York and Paris. More information is available at www.nuxeo.com.