3

This seems like a very basic question but I can't figure it out. Using pnpjs I want to retrieve the 50 most recently created items, oldest first, newest last.

I can only manage to get them in the wrong order:

 const items = await this.spClient.web.lists.getByTitle(this.spList).items
            .top(50)
            .orderBy("Created",false)
            .get();

If I change this to

 const items = await this.spClient.web.lists.getByTitle(this.spList).items
            .top(50)
            .orderBy("Created",true)
            .get();

the order is right but I get the 50 first items.

So how to get the items I want sorted the way I want?

1
  • Not able to reproduce. Your code is working at my end. Which pnp js are you using? Jan 31, 2020 at 12:59

3 Answers 3

5

You will need to do both the REST orderBy, and then use JavaScript to reverse the array of items in the returned results.

The problem is thus: In order for the REST API to return only the n most recent items, you have to provide the ODATA orderBy the created date in Descending order. But, if you want to use or display those same n items in any other order than that which you retrieved, you will need to re-sort the results in your code.

1
  • Thank you. I was afraid I'd have to do this. Now I know.
    – mplwork
    Jan 31, 2020 at 17:51
1

We can also use caml query to get the 50 items and sorted. Below is not an exact working code, I haven't tested, but it will give you an idea to implement it.

const xml = "<View><ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /></ViewFields><Query><OrderBy><FieldRef Name='Created' /></OrderBy></Query><RowLimit>50</RowLimit></View>";    
const q: CamlQuery = {
    ViewXml: xml,
};    
const items = pnp.sp.web.lists.getByTitle("SPPnPJSExampleList").getItemsByCAMLQuery(q)
0

Try using below if this works for you:

const items = await this.spClient.web.lists.getByTitle(this.spList).items
             .orderBy("Created",true)
             .top(50)
             .get();

For descending order you can use orderBy("Created, false).

Update:

As @willman mentioned, You need to get the data in reverse order using orderBy("Created, false) and then you need to reverse the array in your code when you get the complete data.

PnPJS - Working With: Items.

3
  • Also, you can try passing ID instead of Created. Jan 31, 2020 at 13:07
  • Thanks, but this doesn't give me the correct data. It gets the 50 oldest entries sorted from old to new. I want the 50 newest entries sorted from old to new. With .orderBy("Created",false) I get the 50 newest entries but sorted from new to old. Swapping top and orderBy doesn't change anything.
    – mplwork
    Jan 31, 2020 at 17:39
  • Oh, I misunderstood your question. I thought you want the items created first. My bad. Jan 31, 2020 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.