OfficeScripts to list all files properties in OneDrive folder

OfficeScripts does not provide a built-in capability to directly access OneDrive or retrieve a list of files. However, you can use the Microsoft Graph API in conjunction with OfficeScripts to achieve this functionality. The Microsoft Graph API allows you to interact with OneDrive and retrieve file information.

Here’s an example of how you can use OfficeScripts and the Microsoft Graph API to loop through all files in OneDrive and list their names and properties:

async function listOneDriveFiles(linkOrPath) {
  try {
    // Get the access token for authentication
    const accessToken = await context.runtime.getAccessToken();

    // Create the HTTP request to query the Microsoft Graph API
    const url = "https://graph.microsoft.com/v1.0/me/drive/root" + linkOrPath + "/children?$select=name,createdDateTime,modifiedDateTime,size";
    const requestOptions = {
      method: "GET",
      headers: {
        Authorization: "Bearer " + accessToken
      }
    };

    // Execute the request and get the response
    const response = await fetch(url, requestOptions);
    const data = await response.json();

    // Process the file information
    const files = data.value;
    files.forEach(file => {
      console.log("File Name: " + file.name);
      console.log("Created Date: " + file.createdDateTime);
      console.log("Modified Date: " + file.modifiedDateTime);
      console.log("Size: " + file.size);
      console.log("------------------------");
    });

  } catch (error) {
    console.log("Error occurred: " + error);
  }
}

In this example, the listOneDriveFiles function takes a linkOrPath parameter, which represents the link or path of the folder within OneDrive that you want to retrieve the files from. The function uses the context.runtime.getAccessToken() method to obtain the access token for authentication with the Microsoft Graph API.

The function then creates an HTTP GET request to query the Microsoft Graph API using the provided link or path. The request is authorized using the access token retrieved from OfficeScripts. The response is processed to extract the file information such as name, created date, modified date, and size. The information is then printed to the console.

To use this function, you can call it with the desired link or path of the folder in OneDrive as an argument. For example:

listOneDriveFiles("/Documents")
  .catch(function (error) {
    console.log("Error occurred: " + error);
  });

Please note that this example assumes that you have the necessary permissions and access to the OneDrive account. Additionally, you need to have the appropriate authentication and authorization set up to access the Microsoft Graph API.

Keep in mind that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech