Office365-REST-Python-Client
Office365-REST-Python-Client copied to clipboard
When I try to get the SharePoint list items, it sometimes fails with error message: 'int' object is not subscriptable
When I try to get the SharePoint list items, it sometimes fails with error message: 'int' object is not subscriptable This error doesn't happen very often, about 1-2 times every 10 times .
below is my Code :
client_credentials = ClientCredential(client_id = 'XXXXX', client_secret ='XXXXX')
ctx = ClientContext('https://xxx.com').with_credentials(client_credentials)
listObject = ctx.web.lists.get_by_title('SharePointName')
try:
SharePoint_items = SharePoint_ListObject.items.top(10000)
ctx.load(SharePoint_items)
if SharePoint_items != None:
ctx.execute_query()
except Exception as ErrorMSG:
GLM.writeErrorInfoToTXT("ERR_CODE[19]: " + str(ErrorMSG))
I'm very curious about this kind of error that happens every now and then, hope this helps
@vgrem would you pls help for this ? thank you .
Greetings,
this line:
if SharePoint_items != None:
looks rather suspicious, namely items collection is not yet getting populated since they are not requested from server at this moment.
If you are after to determine whether list contains any items, the following approach could be considered:
items = list_object.items.top(100).get().execute_query()
if len(items) == 0:
print("No items were found...")
Greetings,
this line:
if SharePoint_items != None:looks rather suspicious, namely items collection is not yet getting populated since they are not requested from server at this moment.
If you are after to determine whether list contains any items, the following approach could be considered:
items = list_object.items.top(100).get().execute_query() if len(items) == 0: print("No items were found...")
thank you , vgrem . how to do if I want to read all of the list items ? there are over 10000+ items in the list
Greetings,
regarding
how to do if I want to read all of the list items ? there are over 10000+ items in the list
in 2.3.14 version has been introduced ClientObjectCollection.get_all method to retrieve all the items in a collection, regardless of the size, for example:
def print_progress(items):
"""
:type items: office365.sharepoint.listitems.collection.ListItemCollection
"""
print("Items read: {0}".format(len(items)))
page_size = 5000 # default, the list view threshold
ctx = ClientContext(site_url).with_credentials(client_credentials)
large_list = ctx.web.lists.get_by_title(list_title)
all_items = target_list.items.get_all(page_size, print_progress).execute_query()