0

I have a web part in SPFx for SharePoint Online which displays list items and any attachments on those list items. The code seems to take quite a while (maybe a third of a second per attachment)

Is there any way to speed things up?

Thanks P

        ViewXml: this.properties.camlQuery,
    });

      

    // look through the returned items.
    for (var i = 0; i < r.length; i++) {


console.log('ID:' + r[i]["ID"]);



                //const item: IItem = sp.web.lists.getByTitle(this.properties.listName).items.getById(Number(r[i]["ID"]));

                console.log('got by id');

                // get all the attachments
                const attachmentInfo: IAttachmentInfo[] = await item.attachmentFiles();

                console.log('got attachmentInfo');

                 attachmentInfo.map(file=>{
                   var fileUrl=file.ServerRelativeUrl;
                   console.log('fileUrl:' + fileUrl);
                 })

Pete Whelan
  • 85
  • 11

1 Answers1

1

Try batching: https://pnp.github.io/pnpjs/concepts/batching/

It just packs multiple requests into a single request, so you could get data for all your items at once (with a single SP request, that is)

const [batchedSP, execute] = sp.batched();
for (var i = 0; i < r.length; i++) {
 const item = batchedSP.web.lists.getByTitle(this.properties.listName).items.getById(Number(r[i]["ID"]));
  item.attachmentFiles().then(attachmentInfo => {
    console.log('got attachmentInfo');
    // do something with attachment info
  }
}
await execute(); // <<< the real request is sent here
Nikolay
  • 10,752
  • 2
  • 23
  • 51