Pnpjs: How do I execute getNext from a PagedItemCollection as part of a batch request?

Created on 9 Mar 2018  路  7Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [ 1.0.3 ]

Expected / Desired Behavior / Question

In this scenario, I want to get items from multiple lists using inBatch but paging each request so I can return the responses and use them to get the next batch of results, i.e:

let results: any[] = [];
let responses: PagedItemCollection[] = [];
const getItems = sp.web.createBatch();

sp.web.lists.getByTitle('List 1').items.top(5).inBatch(getItems).getPaged().then(response => {
  results = results.concat(response.results);
  responses = responses.concat(response);
});
sp.web.lists.getByTitle('List 2').items.top(5).inBatch(getItems).getPaged().then(response => {
  results = results.concat(response.results);
  responses = responses.concat(response);
});

await getItems.execute();

The results and responses are collected to be returned to the calling function. My question is, how do I use the responses to batch the getNext functions from each of them into a single request? From what I can tell, PagedItemCollection does not inherit from ODataQueryable, as such inBatch is not a public member of PagedItemCollection.

code answered question

All 7 comments

Paging is not supported as part of batching by design currently. That being said we can take a look and see what it would take to enable this. Might not be possible but I see what you are suggesting. Thanks!

ok thanks for confirming @patrick-rodgers!

Hi @nero120 - I am circling back and looking at issues today. I misspoke, this will actually work as-is in the library as shown below. Meaning you can already do what you wanted :)

const b = sp.web.createBatch();

// get first set of results as part of a batch
sp.web.lists.getByTitle("BigList").items.inBatch(b).getPaged().then(result => {

    console.log(result.results.length);

    // get the second set of results from the first result instance
    result.getNext().then(result2 => {

        console.log(result2.results.length);
    });
});

b.execute();

@patrick-rodgers unfortunately you haven't answered the question as the second request is not issued as part of a batch request (i.e. the second request should be triggered by another call to b.execute()). If you were collecting results from multiple lists, your example implies that getNext for each list would need to be executed individually rather than part of a batch.

Can you please clarify and re-open if necessary?

Ok, I didn't understand that part then. Can you explain the use case where you would be looking to page through a collection in batches like this? I am not sure I understand the gain in doing so. If you are looking to get all the items in several lists I would recommend getAll().

Actually thinking about this more, my particular use case may not be a good example of the need to batch across multiple lists. I think the correct solution in my case is to combine into one list and just use separate content types (this is an upgrade to a legacy system which used separate lists, incorrectly imho). In that case, the current pnp batching functionality works fine and I can't really present a case for batching across multiple lists. There may be a good use case out there, in which case perhaps someone else who would want this functionality could contribute it?

In any case, I'll withdraw my request and close this. Thanks for your help @patrick-rodgers.

Ok, thanks for the feedback and let us know if we can help out in the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahsanranjha picture ahsanranjha  路  3Comments

AJIXuMuK picture AJIXuMuK  路  3Comments

DennisGaida picture DennisGaida  路  3Comments

KieranDaviesV picture KieranDaviesV  路  3Comments

jcosta33 picture jcosta33  路  3Comments