sharepoint-onlinethresholdpnp-jssplistitemsplist

How to fetch more than 5000 item from SharePoint Online list using @pnp/pnpjs library?


I am using SharePoint Online. I am using "@pnp/pnpjs" library to get data from the SharePoint Online list.

I have created a SharePoint list that contains more than 5000 items.

How can I get all list items using the get function from @pnp/pnpjs library?

How can I filter items based on the condition while getting data from the SharePoint online list using @pnp/pnpjs library?

Thanks


Solution

  • Using the items collection's getAll method you can get all of the items in a list regardless of the size of the list. Sample usage is shown below. Only the odata operations top, select, and filter are supported. usingCaching and inBatch are ignored - you will need to handle caching the results on your own. This method will write a warning to the Logger and should not frequently be used. Instead the standard paging operations should be used.

    import { spfi } from "@pnp/sp";
    import "@pnp/sp/webs";
    import "@pnp/sp/lists";
    import "@pnp/sp/items";
    import "@pnp/sp/items/get-all";
    
    const sp = spfi(...);
    
    // basic usage
    const allItems: any[] = await sp.web.lists.getByTitle("BigList").items.getAll();
    console.log(allItems.length);
    
    // set page size
    const allItems: any[] = await sp.web.lists.getByTitle("BigList").items.getAll(4000);
    console.log(allItems.length);
    

    Source: https://pnp.github.io/pnpjs/sp/items/#get-all-items