I don’t know why but this took a lot longer than it should. GitHub GoPilot kept giving me code that didn’t work and then getting stuck in a loop when I tried to make it work. Code I found online simply didn’t work either. So what’s the big deal? I think I just misunderstood how this all worked.

Let’s break it down. First is the sharepoint_base_url which is the URL that we’ll ask for folders and files relative to. Second is the sharepoint_folder that you request folders and files from, this is relative to the base url.

So for your own user’s Shared Documents, it would be:

sharepoint_base_url = 'https://my-sharepoint.sharepoint.com/'

And to get your Shared Documents, your sharepoint_folder would be:

sharepoint_folder = 'Shared%20Documents/'

IF you want to access a subsite, then you add it to the sharepoint_base_url. So for example like this:

sharepoint_base_url = 'https://my-sharepoint.sharepoint.com/sites/SomeOtherDepartment'

And then the sharepoint_folder remains the same, but now you’ll be accessing the Shared Documents of that subsite, not your own content.

So how can your find the right URL to use? If this is a folder that is shared with you on Sharepoint you can navigate to it online and browse the documents in the folder you want, you’ll see your URL is something like this:

https://my-sharepoint.sharepoint.com/personal/my-username_my-company-name/_layouts/10/onedrive.aspx?isAscending=false&id=%2Fsites%2FSomeOtherDepartment%2FShared%20Documents&listurl={etc}

Here you can see that the sharepoint_base_url we need is {your sharepoint base} /sites/SomeOtherDepartment

And the relative folder will be Shared%20Documents/

Things to look out for are that the sharepoint_base_url ends with a slash, and the sharepoint_folder also does, but does not begin with a slash! Also remember to escape the sharepoint_folder, so %20 for space etc. Also, check your user has permissions to access the folder…

This Python code is based on some I found on GitHub (apologies I didn’t make a note of who) and was then mashed up with GitHub CoPilot and myself.

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

# SharePoint URL and credentials 
sharepoint_base_url = 'https://my-sharepoint.sharepoint.com/sites/SomeOtherDepartment/'
sharepoint_user = 'my-username_my@company-name.com'
sharepoint_password = 'my-password'

# Authentication
auth = AuthenticationContext(sharepoint_base_url)
auth.acquire_token_for_user(sharepoint_user, sharepoint_password)
ctx = ClientContext(sharepoint_base_url, auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print('Connected to SharePoint: ',web.properties['Title'])

# Get file details in SharePoint Folder
def folder_details(ctx, sharepoint_folder):
    # get the folder
    folder = web.get_folder_by_server_relative_url(sharepoint_folder)

    # load the folder's properties
    ctx.load(folder)
    ctx.execute_query()

    # print the folder's properties
    print("Folder properties:")
    print("Name: ", folder.properties['Name'])
    print("ServerRelativeUrl: ", folder.properties['ServerRelativeUrl'])

    # get and print the folder's files
    files = folder.files
    ctx.load(files)
    ctx.execute_query()
    print("\nFiles:")
    for file in files:
        print(file.properties['Name'])

    # get and print the folder's subfolders
    subfolders = folder.folders
    ctx.load(subfolders)
    ctx.execute_query()
    print("\nSubfolders:")
    for subfolder in subfolders:
        print(subfolder.properties['Name'])
 
# Call func
sharepoint_folder = 'Shared%20Documents/'
file_list = folder_details(ctx, sharepoint_folder)
print(file_list)

Last modified: October 25, 2024

Author

Comments

Write a Reply or Comment

Your email address will not be published.