python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

How to upload a file into a SharePoint?

Open pigna90 opened this issue 6 years ago • 8 comments

I've been using O365 for a few days and I'm trying to understand how to upload a new file into a SharePoint.

I've tried to call this method on a O365.sharepoint.Site object but I don't know if this is the right way.

Any advice? :)

pigna90 avatar Mar 10 '20 17:03 pigna90

You first need to get the site with get_site. Then call get_document_library on the site instance.

With the returned object you can upload and download files and etc.

Check the readme and also the code files (drive.py)

alejcas avatar Mar 10 '20 17:03 alejcas

Thanks! This is exactly what I was searching for, but it's not clear where I can retrieve the drive_id for get_documet_library. Any suggestions?

pigna90 avatar Mar 11 '20 08:03 pigna90

I recommend you to always check the source code. If you look at the sharepoint.py file under the Site object you will find the methods you need:

  • get_default_document_library: to get the default document library of the site. 80% of sites only have 1 document library.
  • list_document_libraries: list the drives this site has.

A document library is just a onedrive Drive.

alejcas avatar Mar 11 '20 08:03 alejcas

Many thanks! It works perfectly, I'll update the readme file if it's ok for you.

pigna90 avatar Mar 11 '20 09:03 pigna90

I'll update the readme file if it's ok for you.

Of course! PR are always welcome

alejcas avatar Mar 11 '20 09:03 alejcas

@pigna90 @janscas I'm struggling to figure out how to download a file as well. An update to the read me that explains in detail how to upload and download files on a SharePoint would be much appreciated.

I have spent the past approx. 10 hours reading through the SharePoint and drive source code and still can't figure out how to download a file from a folder that is nested within a folder within a folder on my SharePoint.

O365 has so much potential just needs some read me improvements.

Here is my code...

share_point = account.sharepoint()

# I was able to find my SharePoint site_id by using this link as a reference point
# I have no clue how to get the site_id without using this link I couldn't figure that out either
# https://support.cloudhq.net/how-to-find-guids-of-sharepoint-sites/
# this code prints the name of my SharePoint
site = share_point.get_site(site_id)
# this code prints my default drive
my_site = site.get_default_document_library()
# this code shows me my document libraries
default_folders = site.list_document_libraries()
# above code print output looks like this [Drive: Documents, Drive: Preservation Hold Library, Drive: Teams Wiki Data]
# I have no clue how to actually select these folders to dive deeper into my SharePoint
# nor do I know how to get the actual drive id for these items in this list

If either of you could provide some insight it would be greatly appreciated. I also will assist in updating the docs if I can get a quick explanation on how to dive deeper into the layers of folders on my SharePoint and how to find the drive_id.

jjr5330 avatar Mar 18 '20 04:03 jjr5330

The sharepoint utilities are mainly done by collaborators and lacks a lot of documentation details. I hope to be able to improve this in the future. PR are always welcome if you wan't to contribute.

I was able to find my SharePoint site_id by using this link as a reference point

You can always use ´search_site´ from a the sharepoint instance to look for sites and then retrieve the site id with site.object_id.

my_site = site.get_default_document_library()

get_default_document_library returns a drive instance so its not my_site. It's "my_drive".

default_folders = site.list_document_libraries()

list_document_libraries returns a list of drives. so it's not default_folders. It's "all_drives".

If you follow the code files you will find out this very rapidly. Look into drive.py.

Here is an example:

my_site = site.get_default_document_library()
my_drive = my_site.get_default_document_library()
root_folder = my_drive.get_root_folder()
child_folders = root_folder.get_child_folders()

for folder in child_folders:
    for item in folder.get_items():
        print(item)
        if item.name == 'the_filename_i_want':
            item.download('destination_folder')

# you can also do a folder.download_contents('to_folder').

alejcas avatar Mar 18 '20 09:03 alejcas

@janscas Your short explanation helped a ton. I finally was able to figure out how to download a file! :) I will gladly assist with documentation. I may open up a couple other issues while I run through different SharePoint use cases that may need answering. This will allow me to compile code for the documentation. @pigna90 happy to collaborate on this if you've already started on something.

jjr5330 avatar Mar 19 '20 13:03 jjr5330