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.
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:
pip install botcity-ms365-onedrive-plugin
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
from botcity.plugins.ms365.onedrive import MS365OneDrivePlugin
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)
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.
onedrive = MS365OneDrivePlugin(service_account=service) # Getting the files and folders from the root folder drive_files = onedrive.get_files() for file in drive_files: print(file.name)
Using a specific folder as a reference, let's pass the path of the folder on the drive as a parameter.
The path must follow the pattern: /path/to/parent_folder.
/ 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.
onedrive = MS365OneDrivePlugin(service_account=service) # Getting files from parent folder 'subfolder' drive_items = onedrive.get_files_from_parent_folder(folder_path="/myFolder/subFolder") for item in drive_items: if item.is_file: item.download()
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)
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.
onedrive = MS365OneDrivePlugin(service_account=service) # Uploading file onedrive.upload_file( local_file_path="C:\Downloads\image.png", destination_folder_path="/myFolder/subFolder" )
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.
onedrive = MS365OneDrivePlugin(service_account=service) # Downloading file onedrive.download_file( file_path="/myFolder/test.txt", to_path=".\downloaded_files" )
As in the examples above, using the file path on the drive we can delete a specific file.
onedrive = MS365OneDrivePlugin(service_account=service) # Deleting file onedrive.delete_file(file_path="/myFolder/subFolder/document.pdf")
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.
onedrive = MS365OneDrivePlugin(service_account=service) # Creating a new folder onedrive.create_folder(folder_name="mySubFolder", create_on_path="/myFolders")
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.