Office365-REST-Python-Client
Office365-REST-Python-Client copied to clipboard
Lists yes, items no
I'm trying to dump a SP List into a xslx. Connection works fine, i can see all lists... but i cannot pull the items from any list. The get_items() just returns 0 or None. I tried just about all methods presented here. My last attempt is with the 'view'
list_source = ctx.web.lists
ctx.load(list_source)
ctx.execute_query()
for l in list_source:
# print(l.properties['Title']) # up to here i get all the lists but not the items
if l.properties['Title'] == 'mylist_title':
view_fields = list_source.views.get_by_title('Default').view_fields.get().execute_query()
fields = [list_source.fields.get_by_internal_name_or_title(field_name).get() for field_name in view_fields]
ctx.execute_batch()
fields_json = {f.internal_name: f.title for f in fields}
print(json.dumps(fields_json))
This gives me this error:
view_fields = list_source.views.get_by_title('Default').view_fields.get().execute_query()
AttributeError: 'ListCollection' object has no attribute 'views'
If i go like this:
ctx = ClientContext(app_settings['url'], ctx_auth)
ctx.load(ctx.web)
lists = ctx.web.lists
ctx.load(lists)
ctx.execute_query()
for l in lists:
if l.properties['Title'] == 'mylist_name':
print(l.properties['Title']) # up to here i get all the lists but not the items
items = l.items.get_items_count()
items = l.get_items()
ctx.load(items)
ctx.execute_query()
print(items)
I do get this:
<office365.sharepoint.listitems.listItem_collection.ListItemCollection object at 0x000002198B3B7730>
If switch the print to print(items.properties) I'm just getting an empty {}
How do i pull the items and dump them into a xlsx file? (I need xlsx as csv messes up some characters) Or maybe in a dataframe (and from there i can dump it myself into an excel)
items is a List. To work with the individual item can do something like:
for item in items:
print(item.properties)
Greetings,
it appears the confusion comes from the fact that View.view_fields
property returns:
Gets a the collection of field names in the list view
To retrieve items (field values) from a list view the View.get_items()
is intended, for example:
list_title = "Documents"
view_title = "All Documents"
list_view = ctx.web.lists.get_by_title(list_title).views.get_by_title(view_title)
list_items = list_view.get_items().execute_query()
The following example demonstrates how to export items from a list view into a csv file:
ctx = ClientContext(site_url).with_credentials(credentials)
list_title = "Documents"
view_title = "All Documents"
list_view = ctx.web.lists.get_by_title(list_title).views.get_by_title(view_title)
export_items = list_view.get_items().execute_query()
export_path = os.path.join(tempfile.mkdtemp(), "DocumentsMetadata.csv")
export_to_csv(export_path, export_items)
where
def export_to_csv(path, list_items):
"""
:param str path: export path
:param office365.sharepoint.listitems.collection.ListItemCollection list_items: List items
"""
with open(path, 'w') as fh:
fields = list_items[0].properties.keys()
w = csv.DictWriter(fh, fields)
w.writeheader()
for item in list_items:
w.writerow(item.properties)