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

request for an example to retrieve a sharepoint list into a table

Open InitialXW opened this issue 6 years ago • 2 comments

I am new to O365, i have searched the doc and the issues sections but could not get my code to read from a sharepoint list into a table-like structure for further processing, wonder if i am missing anything obvious here. the below is the code i have so far, any help is greatly appreciated, thanks:

resultlist = myspsite.get_list_by_name('my list name') items = resultlist.get_items() for item in items: print(item.data)

then i get:

[List Item: https://mysp.sharepoint.com/sites/SITENAME/NAME/Lists/MYLISTItems/1289_.000, List Item: https://mysp.sharepoint.com/sites/SITENAME/NAME/Lists/MYLISTItems/1290_.000, ...] what do i need to do from here to read the list/items into e.g. a pandas table?

InitialXW avatar Jul 23 '20 07:07 InitialXW

I faced the same issue. I believe you should be able to print item.fields instead of item.data and get the actual data returned. However, this feature seems to be broken as I couldn't get it to work either. It returns None values for every record me.

This is how I was able to build a dataframe with the SharePoint list data. I would really like feedback if anyone else has done this in an easier way because I think there should definitely be an easier way. Or if someone can find where the code is broken in SharePoint list .get_items() method and could mimic the get_item_by_id() method's fields output. It seems like the SharepointListItem class is not allowing for field values to be returned for some reason.

Note: I use pandas to create the dataframe if you are unfamiliar it is an awesome python tool for reading in data in a table like structure.

# use sharepoint site to get list by name
share_point_list = site.get_list_by_name('INSERT YOUR LIST NAME HERE')

# get share point list items
list_items = share_point_list.get_items()

# create a list of object_id values for all sharepoint list item rows
id_list = []

for item in list_items:
    id_list.append(item.object_id)

# create a list of share point list item field value dictionaries using the ids
# note you will use the share point list object we created earlier
list_row_values = []

for item_id in id_list:
    list_row = share_point_list.get_item_by_id(item_id)
    list_row_values.append(list_row.fields)

# create pandas dataframe from list_row_values
df = pd.DataFrame(list_row_values)

# save dataframe to csv if needed
df.to_csv(R"insert_file_path\sharepoint_list.csv", index=False)

jjr5330 avatar Jul 28 '20 17:07 jjr5330

Thanks so much for the help! I echo your wonder on the use of ".field" and I got everything returned with "None' value too. This is exactly where i scratched my head and wondered if i missed anything obvioius. And your code works great except that when the size of the list is big, the looping time is a bit of too long. but I guess this is the best walkaround there is to this problem. thanks again for your help, highly appreciated!

InitialXW avatar Jul 29 '20 08:07 InitialXW