Created
January 27, 2021 01:08
-
-
Save svarukala/49a25565723e96b7e427d32be4ef37af to your computer and use it in GitHub Desktop.
Python sample to upload small to medium sized files to SharePoint Online document libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from office365.runtime.auth.client_credential import ClientCredential | |
| from office365.sharepoint.client_context import ClientContext | |
| client_id = '--clientid--' | |
| client_secret = '--clientsecret---' | |
| site_url = 'https://contoso.sharepoint.com/sites/Web01' | |
| credentials = ClientCredential(client_id, | |
| client_secret) | |
| ctx = ClientContext(site_url).with_credentials(credentials) | |
| target_web = ctx.web.get().execute_query() | |
| print(target_web.url) | |
| path = "SampleDoc.pdf" | |
| with open(path, 'rb') as content_file: | |
| file_content = content_file.read() | |
| list_title = "Documents" | |
| target_folder = ctx.web.lists.get_by_title(list_title).root_folder | |
| name = os.path.basename(path) | |
| target_file = target_folder.upload_file(name, file_content) | |
| ctx.execute_query() | |
| print("File url: {0}".format(target_file.serverRelativeUrl)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment