0

I want to get the list of all files in my personal oneDrive/sharepoint within my company via python. However, I only get an empty list as a return without any errors.

My code looks as follows (taken from Sharepoint_files_python:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.files.file import File 
import io

user = 'user'
pw = 'pw'

url = 'https://company-my.sharepoint.com'
rel_url = '/personal/name_company/Documents/further/path/to/folder/'

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(user, pw):
  ctx = ClientContext(url, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print('Authenticated into sharepoint as: ',web.properties['Title'])

else:
  print(ctx_auth.get_last_error())

  
folder = ctx.web.get_folder_by_server_relative_url(rel_url)
fold_names = []
sub_folders = folder.files #Replace files with folders for getting list of folders
ctx.load(sub_folders)
ctx.execute_query()
     
for s_folder in sub_folders:            
   fold_names.append(s_folder.properties["Name"])

print(fold_names)

This gives an empty list as an output: []

And everything else I try to access within the ctx.web is empty as well, e.g.:

root_folder = ctx.web.default_document_library().root_folder
ctx.load(root_folder,['Files'])
ctx.execute_query()
for f in root_folder.files:
    print(f.name)

[]

My expected outcome would be: ['file.xlsx','file_35.xlsx',....]

However, I can directly access individual files (but these are updated and change and thus not practical for hardcoding) in that folder via:

url = 'https://company-my.sharepoint.com/personal/name_company/Documents/further/path/to/folder/file.xlsx'

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(user, pw):
    ctx = ClientContext(url, ctx_auth)
    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print("Authentication successful")
else:
    print(ctx_auth.get_last_error())

resp = File.open_binary(ctx,url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(resp.content)
bytes_file_obj.seek(0) #set file object to start

df = pd.read_excel(bytes_file_obj,engine='openpyxl',header=0)
print(df)

I get the expected outcome of the file being read and being available in python.

Does anyone know why there is no error when executing the on full sharepoint working code and how I can retrieve the list of files in my folder?

2
  • In your code, you try to ctx.web.get_folder_by_server_relative_url(folder_url) when variable folder_url hasn't been assigned to. I think this would be returning an error if you're running the code you've pasted here, which raises the question of whether the code you've shown is what you're actually running? Commented Aug 20, 2024 at 11:09
  • Thanks, that was a copy paste error from the link I mentioned. That should be rel_url. I will correct it above. Commented Aug 20, 2024 at 12:04

1 Answer 1

0

I'm not familiar with the office365 library you're trying to use, but I can give an example of how you could do this using SharePlum:

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

authcookie = Office365('https://abc.sharepoint.com', username='[email protected]', password='password').GetCookies()
site = Site('https://abc.sharepoint.com/sites/MySharePointSite/', version=Version.v2016, authcookie=authcookie)

rel_url = '/personal/name_company/Documents/further/path/to/folder/'

myfolder = site.Folder(rel_url)

fold_names = []

for f in myfolder.files:
    fold_names.append(f["Name"])

print(fold_names)

You will probably have to install SharePlum to your environment first.

Note that importing Version and passing version=Version.v2016 may or may not be appropriate for your situation. Looks like this argument is optional and defaults to Version.v2007. Apologies, but I'm actually not sure how to check which one you will need so hopefully this doesn't cause a headache.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, the connection worked, hower I get the following error message with any version: Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url: company-my.sharepoint.com/_api/web/folders It might be because I do not have any "/sites/MySharePointSite/" or atleast I do not know how to access it.
You might be able to use a web browser to visit company-my.sharepoint.com and then click the globe-like icon on the left to view a list of your sites. Clicking on one of these will then take you to company-my.sharepoint.com/sites/MySharePointSite. You could then pass that URL when creating your Site object and then potentially also tweak the folder path if required
Unfortunately this does not work. At "company.sharepoint.com" there is a globe but I do not have any permissions there/my files are stored at my personal one. At my personal one "company-my.sharepoint.com" there is no globe and thus no /sites/MySharePointSite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.