pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
751 stars 304 forks source link

getall() and getpaged() not working in the new pnp update #3141

Open sahil5724 opened 1 day ago

sahil5724 commented 1 day ago

What version of PnPjs library you are using

4.x

Minor Version Number

4.5.0

Target environment

SharePoint Framework

Additional environment details

i was using pnp 3.7 and all before but now due to the recent update of pnp 4 many things in my project has stopped working such as the getpaged and getall as i am trying to fetch more than 5k+ data from sharepoint list

Question/Request

I'm struggling now to fetch all items using getall and getpaged due to new update of pnp below are the methods i am using

image

public async getData(): Promise { try { const web = Web(this.props.context.pageContext.web.absoluteUrl); const items = await web.lists.getByTitle("DataAdditionExample").items .select("Id", "CustomerName", "ProductName", "ProductDescription") .getAll();

  const filteredData = items.map((item: any) => ({
    Id: item.Id,
    CustomerName: item.CustomerName,
    ProductName: item.ProductName,
    ProductDescription: item.ProductDescription
  }));

  this.setState({
    items: filteredData,
    ListData: JSON.stringify(filteredData, null, 2),
    isDataLoaded: true,
  }, () => {
  });
} catch (error) {
  console.error("Error fetching data: ", error);
}

}

getting the error as :

Property 'getAll' does not exist on type 'IItems'

juliemturner commented 1 day ago

We added documentation about this transition in the migration docs

sahil5724 commented 17 hours ago

We added documentation about this transition in the migration docs

hey julie i just went through the doucment i have a query though

image

in the above image i used the shown imports too but still i am getting an issue in the sp stating that

Cannot find name 'sp'.

could you please help me with it

sahil5724 commented 16 hours ago

and also i tried adding the necessary imports to it and tried using pageditems and getall also but still i am facing some issues below i have mentioned the code and the imports that i used

`import { spfi, SPFx, IItems, DefaultInit, PagedItemCollection } from '@pnp/sp';
import "@pnp/sp/items";
import "@pnp/sp/webs";
import "@pnp/sp/lists";`

and below is the code that i wrote:

    public async function: any getAllListItems(): Promise<ListItem[]> {
    const sp = spfi().using(DefaultInit());
    const listTitle = "DataAdditionExample";
    const pageSize = 1000; 

    let allItems: ListItem[] = [];
    let pagedItems: PagedItemCollection<IItems>;

    try {
      pagedItems = await sp.web.lists.getByTitle(listTitle).items.top(pageSize)();

      allItems = [...pagedItems.results];

      while (pagedItems.hasNext) {
        pagedItems = await pagedItems.getNext();
        allItems = [...allItems, ...pagedItems.results];
      }

      console.log('Total items fetched:', allItems.length);
      return allItems;
    } catch (error) {
      console.error('Error fetching list items:', error);
      return [];
    }
  }

  getAllListItems().then(items => {
    console.log('Fetched Items:', items);
});

the error i am facing is with : getAllListItems

Expected '=' for property initializer.ts(1442) (method) PersonalUse.getAllListItems(): Promise<ListItem[]>

your input in this would be help full

juliemturner commented 11 hours ago

For future reference when adding a block of code to a comment please put three backticks (and ideally the language) and then your code, and then three backticks to close the code block so that it formats correctly and is easier to read. I fixed your comment above so you can see an example.

I really don't have enough information here to help you. First the earlier comment about Cannot find name 'sp'. , if you're migrating from version 3 then the sp object is the same factory interface that you created for version3, so if it was working then it should still be working.

In your second comment you seem to be redefining the sp object yet again, and you're using a method that's not appropriate for SPFx ( see documentation ) . So, seems like you need to go back to the original way you were creating the sp object and if you want a separate method to get the items you either need a class where the sp object is defined locally or you need to pass the sp object in from wherever you're calling it from.