Classify files in OneDrive Business using API

Copper Contributor

My goal is to add some label/tag to files through API. I tried creating sensitivity labels but they are only supported for certain file types. Then I tried retention labels without any retention policy just to classify files but I am not able to apply these labels through api. I want to be able to apply retention labels to files in my OneDrive business account through my application in node.js which uses a daemon application for authentication. How can I achieve this? Or is there any other way I can classify files using API?

1 Reply

@suryagarg 

Microsoft's APIs and capabilities might have evolved since then.

Here is the general guidance on how you could achieve classifying files in OneDrive for Business using APIs:

To classify files in OneDrive for Business using retention labels through API, you typically need to use the Microsoft Graph API. Retention labels are part of Microsoft 365's information governance and compliance features. Here are the general steps to achieve this:

  1. Create a Retention Label in Microsoft 365 Compliance Center: First, create a retention label that represents the classification you want to apply to files. Make sure it is configured to be manually applied and doesn't have any retention policy if you only want to use it for classification.
  2. Authenticate Your Application: Set up your Node.js application with the necessary permissions and authentication to access the Microsoft Graph API. You mentioned using a daemon application for authentication, which is a common approach for server-to-server communication.
  3. Use Microsoft Graph API to Apply Label to Files: Use the Microsoft Graph API to apply the retention label to specific files in your OneDrive for Business account. You can use the Update method of the DriveItem resource to set the sensitivityLabel property, which also works for applying retention labels.

Here is a simplified example of how you might use the Microsoft Graph API in Node.js to update a file with a retention label:

const axios = require('axios');

const accessToken = 'YOUR_ACCESS_TOKEN';
const fileId = 'FILE_ID';
const labelId = 'RETENTION_LABEL_ID';

const headers = {
  Authorization: `Bearer ${accessToken}`,
  'Content-Type': 'application/json'
};

const data = {
  sensitivityLabel: {
    id: labelId
  }
};

axios
  .patch(`https://graph.microsoft.com/v1.0/me/drive/items/${fileId}`, data, { headers })
  .then(response => {
    console.log('Label applied successfully:', response.data);
  })
  .catch(error => {
    console.error('Error applying label:', error);
  });

 

Please note that this is a simplified example, and you should handle errors, token expiration, and authentication securely in your actual application. Also, refer to the latest Microsoft Graph API documentation for any changes or updates to the API endpoints and data structures.

Before implementing this solution, I recommend checking the latest documentation and resources related to Microsoft 365 compliance features and the Microsoft Graph API, as there might have been updates or changes since my last update.

 

My knowledge of this topic is limited, but since no one has answered it for at least one day or more, I entered your question in various AI.The text and the steps are the result of various AI's put together. Therefore, I have this solution proposal /Suggestions/Steps for you.

 

My answers are voluntary and without guarantee!

 

Hope this will help you.

 

*If you liked my answer, please like this one.

If my answer helped, please mark it as the correct answer.

This will help several users here.