Office365-rest-python-client: No folder.files output/no error

Created on 13 Mar 2019  ·  12Comments  ·  Source: vgrem/Office365-REST-Python-Client

Hi,

my goal is to print out contents of a sharepoint site document library/list
and I'm trying to figure out why I'm getting no ouput/no error whatsoever.

My url https://my.jci.com/sites/FRAReporting/Shared%20Documents/Forms/AllItems.aspx
points to Documents containing 2 folders and 1 file but I can't seem to see it by running this script.
Changing list_object from "Shared Documents" to alternative titles makes no difference.

Am I missing something obvious here, please? Any help would be much appreciated.

Thank you.

Marcel

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

app_settings = {
'url': 'https://my.jci.com/sites/FRAReporting/Shared%20Documents/Forms/AllItems.aspx',
'client_id': 'f638a093-aa8f-48c7-a4e6-998d7d1ee388',
'client_secret': 'xysjblahblah',
}

if __name__ == '__main__':
context_auth = AuthenticationContext(url=app_settings['url'])
if context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret']):

    ctx = ClientContext(app_settings['url'], context_auth)

    list_object = ctx.web.lists.get_by_title("Shared Documents")
#list_object = ctx.web.lists.get_by_title("Documents")
#list_object = ctx.web.lists.get_by_title("FRAReporting")

    folder = list_object.root_folder        
    ctx.load(folder)
    ctx.execute_query()

    files = folder.files
    ctx.load(files)
    ctx.execute_query()

    for myfile in files:
        print("File name: {0}".format(myfile.properties["Name"]))

All 12 comments

btw using Office365-REST-Python-Client (2.1.1) and python 3.6.4

Greetings!

Unfortunately i'm not able to spot any issues with the provided example, everything looks good for me.

From the below description:
_points to Documents containing 2 folders and 1 file_

could it be files are not stored under beneath root folder but under sub folder instead?

Anyway, could you please give a try this approach just to make sure whether items are returned or not?

list_obj = context.web.lists.get_by_title(list_title)
qry = CamlQuery.create_all_items_query()
items = list_obj.get_items(qry)
context.load(items)
context.execute_query()
for cur_item in items:
    print("File name: {0}".format(cur_item.properties["Title"]))

image

Hi,
I get the following when running your code:

PS C:UsersfooAppDataLocalProgramsPythonPython36ScriptsSharePoint> ....python.exe .sharePoint_office365_test4.py
Traceback (most recent call last):
File ".sharePoint_office365_test4.py", line 36, in
ctx.execute_query()
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365runtimeclient_runtime_context.py", line 36, in execute_query
self.pending_request.execute_query()
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365runtimeclient_request.py", line 32, in execute_query
return self.execute_pending_queries()
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365runtimeclient_request.py", line 38, in execute_pending_queries
response = self.execute_request_direct(request)
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365runtimeclient_request.py", line 109, in execute_request_direct
self.context.ensure_form_digest(request_options)
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365sharepointclient_context.py", line 27, in ensure_form_digest
self.request_form_digest()
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesoffice365sharepointclient_context.py", line 39, in request_form_digest
payload = response.json()
File "C:UsersfooAppDataLocalProgramsPythonPython36libsite-packagesrequestsmodels.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:UsersfooAppDataLocalProgramsPythonPython36libjson__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:UsersfooAppDataLocalProgramsPythonPython36libjsondecoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersfooAppDataLocalProgramsPythonPython36libjsondecoder.py", line 362, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
PS C:UsersfooAppDataLocalProgramsPythonPython36ScriptsSharePoint>

When I print out the string in raw_decode exception, it looks like this, please see attachment

Thank you very much for your time.
Marcel

While reviewing for the second time (and looking at the provided screenshot, thanks for that!), I spotted the provided url does not seem to be valid:

app_settings = {
'url': 'https://my.jci.com/sites/FRAReporting/Shared%20Documents/Forms/AllItems.aspx',
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         needs to be site url  

}

The provided Url corresponds to _absolute Url of library view page_ but what actually ClientContext expecting is siteUrl , so instead of:

https://my.jci.com/sites/FRAReporting/Shared%20Documents/Forms/AllItems.aspx

please give a try:

https://my.jci.com/sites/FRAReporting/

You are absolutely right, that was it.
You made my day, can't thank you enough.
m.

Running your script now prints out like 15 rows:

C:UsersfooAppDataLocalProgramsPythonPython36ScriptsSharePoint> ....python.exe .sharePoint_office365_test4.
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None
File name: None

while running my (I mean yours :) original script I get expected result:
File name: Document.docx
File name: access_denied.txt

My query is, how can I list all the files under folder "TEST" from above SS, for example. That's exactly what I am trying to achieve.

I don't know if I could elaborate on my exact problem. Just like you are successfully printing your files in the root (or whatever it is called; Documents):

File name: Document.docx
File name: access_denied.txt

I too can do that. But, want to get the list of files in the folders rather.

Was this page helpful?
0 / 5 - 0 ratings