Pnpjs: using orderBy when calling getAll()

Created on 11 Feb 2019  路  3Comments  路  Source: pnp/pnpjs

Category

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

Version

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

Please specify what version(s) of SharePoint you are targeting: [ Online ]

Expected / Desired Behavior / Question

When using getAll() on an item collection, it appears $select, $filter, $top and $expand odata params are passed through to the API but the function isn't using any $orderBy statements. Is this by design?

Could it be pulled through by modifying line 89 of src/items.ts to include the orderby odata param?

        this.query.forEach((v: string, k: string) => {
            if (/^\$select|filter|top|expand$/i.test(k)) { // add orderby?
                items.query.set(k, v);
            }
        });

Observed Behavior

When using a query such as this:

web.lists.getByTitle('Some list').items.orderBy('Title', true).get().then(response => {
  // etc
})

returned results are correctly ordered, but using a similar query

web.lists.getByTitle('Some list').items.orderBy('Title', true).getAll().then(response => {
  // etc
})

they are not (or at least this doesn't seem to be working for me). I know getAll is a helper method for recursive querying of large datasets, so the use-case for this might be small but was just wondering really if this was by design or if I'm missing something. Thanks!

Steps to Reproduce

Just to clarify the environment I'm working with, it's an SPFX Webpart project - following installed via npm:
@microsoft\generator-sharepoint - v1.7.1
@pnp\sp - 1.2.9

  • initialise an instance of sp with config
  • perform queries as above and observe resulting API query
code answered question

Most helpful comment

Hi @t0mgerman,

Yes, this is by design as getAll and custom sorting are not compatible (well, can work together only on small lists where getAll make no sense).
Get all method allows requesting a large list without throttling issues and this only works when a default ID indexed sorting is applied.

Get all should mostly be used in backend procedures like synchronization or reporting, custom ordering applied when data array is received locally in memory.

All 3 comments

Hi @t0mgerman,

Yes, this is by design as getAll and custom sorting are not compatible (well, can work together only on small lists where getAll make no sense).
Get all method allows requesting a large list without throttling issues and this only works when a default ID indexed sorting is applied.

Get all should mostly be used in backend procedures like synchronization or reporting, custom ordering applied when data array is received locally in memory.

You can also see in the code where we filter the allowed operators. If you want to get all the items in the list and have orderBy work I would suggest using the paging functionality.

Thanks for the quick reply @koltyakov @patrick-rodgers , I'd tried this purely out of curiosity really - the use case I'm writing for will only bring back a handful of results so I'll be using standard get() anyway - just noticed it while playing around. The reasoning makes total sense, thanks! I'll close as my question's been answered :) 馃憤

Was this page helpful?
0 / 5 - 0 ratings