How to access OneDrive driveItem's properties using Office.js API?

2 min read 06-10-2024
How to access OneDrive driveItem's properties using Office.js API?


Accessing OneDrive DriveItem Properties with Office.js API

Need to extract specific information from files stored in your OneDrive account? The Office.js API provides a robust way to interact with OneDrive data, including accessing and manipulating file properties. This article will guide you through the process, helping you retrieve valuable information about your files.

Scenario: Imagine you have a spreadsheet stored in your OneDrive account and you need to get its file name, size, and last modified date. The Office.js API allows you to achieve this with ease.

Code Example:

Office.onReady(function () {
    Office.context.mailbox.getCallbackTokenAsync(function (result) {
        if (result.status === "succeeded") {
            // Get the OneDrive API endpoint
            var apiEndpoint = "https://graph.microsoft.com/v1.0/me/drive/root/children";

            // Build the request headers
            var headers = {
                "Authorization": "Bearer " + result.value,
                "Content-Type": "application/json"
            };

            // Make a GET request to retrieve the list of files
            $.ajax({
                url: apiEndpoint,
                type: "GET",
                headers: headers,
                success: function (data) {
                    // Find the desired file in the response data
                    var targetFile = data.value.find(function (file) {
                        return file.name === "My Spreadsheet.xlsx"; // Replace with your file name
                    });

                    // Access the file properties
                    if (targetFile) {
                        var fileName = targetFile.name;
                        var fileSize = targetFile.size;
                        var lastModified = targetFile.lastModifiedDateTime;

                        // Display the properties
                        console.log("File name: " + fileName);
                        console.log("File size: " + fileSize);
                        console.log("Last modified: " + lastModified);
                    } else {
                        console.log("File not found");
                    }
                },
                error: function (error) {
                    console.error("Error fetching OneDrive data:", error);
                }
            });
        } else {
            console.error("Failed to get callback token:", result.error);
        }
    });
});

Explanation:

  1. Office.onReady(): This function ensures that the Office.js API is fully loaded and ready for use.
  2. getCallbackTokenAsync(): This function retrieves a callback token from the Office.js API, required for authenticating requests to the OneDrive API.
  3. apiEndpoint: This is the URL for the OneDrive API endpoint that lists files within your OneDrive's root folder.
  4. headers: These headers are used to authenticate the request with the OneDrive API using the callback token.
  5. $.ajax(): This function makes a GET request to the OneDrive API to retrieve a list of files.
  6. data.value.find(): The code iterates through the returned data to find the file with the specified name.
  7. File properties: The code retrieves the desired properties from the target file object:
    • name: The file's name.
    • size: The file's size in bytes.
    • lastModifiedDateTime: The date and time when the file was last modified.

Key Points:

  • Authentication: You need to obtain a callback token from the Office.js API to authenticate requests to OneDrive.
  • API Endpoint: Use the appropriate endpoint depending on your desired location within the OneDrive folder structure.
  • File Properties: The OneDrive API provides a wide range of properties for each file, including name, size, last modified date, created date, file type, and more.

Further Resources:

This guide helps you access and retrieve specific information about your OneDrive files, enabling you to build powerful applications that leverage the power of Office.js and OneDrive APIs.