Microsoft 365 - OneDrive¶
Interact and perform various operations through a Microsoft 365 account. Download, upload, and manage files easily via the BotCity plugin for OneDrive.
Warning
To be able to use this plugin, you need to have a Microsoft 365 account with a properly created and configured project.
After creating a project, you need to complete the authentication process through the Microsoft 365 Credentials plugin.
For more details, see the previous sections:
Installation¶
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
Setting the service account¶
As mentioned above, in order to use the OneDrive plugin, it is necessary to perform the account authentication process through the Microsoft 365 Credentials plugin.
With the credentials plugin instance obtained, let's use it to instantiate the OneDrive plugin.
from botcity.plugins.ms365.credentials import MS365CredentialsPlugin, Scopes
from botcity.plugins.ms365.onedrive import MS365OneDrivePlugin
# Instantiate the Credentials plugin
service = MS365CredentialsPlugin(
client_id='<APPLICATION ID>',
client_secret='<SECRET VALUE>',
)
service.authenticate(scopes=[Scopes.BASIC, Scopes.FILES_READ_WRITE_ALL])
# Instantiate the OneDrive plugin
onedrive = MS365OneDrivePlugin(service_account=service)
Tip
To use the OneDrive service, you can only add the scope Scopes.FILES_READ_WRITE_ALL
referring to the Files.ReadWrite.All
permission of the project.
See more details about scopes and permissions at this link.
Getting some files¶
You can get a list of files stored on the drive. It is possible to get the files from the root folder or from a specific parent folder.
With the returned files, you can perform operations such as downloading and accessing specific properties.
Using a specific folder as a reference, let's pass the path of the folder on the drive as a parameter.
Tip
The path must follow the pattern: /path/to/parent_folder.
The first /
means we are starting from the root folder and the rest of the path will be the "absolute path" of the item on the drive.
Searching for specific files¶
In addition to getting multiple files, it is also possible to filter to return only a specific file.
In this case, we can search for an item in the drive by name or using its absolute path.
onedrive = MS365OneDrivePlugin(service_account=service)
# Returning the first file that matches this name
file = onedrive.get_file_by_name(file_name="credentials.txt")
print(file)
# Getting file by path. Using path in pattern: '/path/to/file'
file_by_path = onedrive.get_file_by_path("/myFolder/subFolder/document.pdf")
print(file_by_path)
Uploading files¶
We can easily upload a new file to OneDrive by passing the local path and the path where the file will be stored.
If no specific path is passed, the file will be saved in the drive's root folder.
Downloading files¶
In addition to downloading files directly through the objects returned by the get_files()
methods, we can also download a file directly by passing its path on the drive.
Deleting files¶
As in the examples above, using the file path on the drive we can delete a specific file.
Creating a new folder¶
We can easily create a new drive folder. The folder can be created in the root folder of the drive or in a specific path.
Managing files through Sharepoint¶
We can perform the same operations with files and folders using the drive of a Sharepoint site as a reference.
See more details about the required configuration in Sharepoint plugin documentation.