Skip to content

Handling Large Lists

Vadim Gremyachev edited this page Nov 11, 2022 · 2 revisions

Retrieving all items in a collection

The library honors the default page size settings which for the most endpoints in Microsoft 365/Graph API is 100 records, meaning the following example

items = large_list.items.items.get().execute_query()

by default will return items for the first page ( which in terms of items endpoint is 100)

If you prefer to return all the items in a collection, ClientObjectCollection.get_all() method could be utilized instead of ClientObjectCollection.get():

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.listitems.listitem import ListItem
from office365.runtime.auth.client_credential import ClientCredential

site_url = "https://contoso.sharepoint.com/"
credentials = ClientCredential('-- client id goes here--', '-- client secret goes here --'))
list_title = "Large List"
page_size = 5000

ctx = ClientContext(site_url).with_credentials(credentials)
large_list = ctx.web.lists.get_by_title(list_title)
all_items = large_list.items.get_all(page_size, print_progress).execute_query()


def print_progress(items):
    """
    :type items: office365.sharepoint.listitems.collection.ListItemCollection
    """
    print("Items read: {0}".format(len(items)))

where page_size corresponds to default query threshold of 5000 items in SharePoint API