SharePoint / PnP-JS-Core

Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Other
379 stars 231 forks source link

Order By is not working properly in PnP JS Core #785

Closed spvjebaraj closed 6 years ago

spvjebaraj commented 6 years ago

Hi, I am using the below PnP code to sort the items but it is not sorting. Please help me to resolve this.

const sortField = 'Title'; const sortAsc = false; let items = sp.web.lists.getByTitle(this.listTitle).items .filter(filterQuery) .select('*', 'Author/ID', 'Author/Title', 'Editor/ID', 'Editor/Title') .expand('Author,Editor') .orderBy(sortField, sortAsc); if (sortField !== 'ID') { items = items.orderBy('ID', false); } items = items.top(pageSize); return Observable.fromPromise(items.getPaged()) .map(res => { res.results = res.results.map(r => this.castAsReportingValue(r)); return res; });

From the above code, if I set the "sortField" to "Created" column it is working fine. For Title field it is not working. Kindly share your inputs.

patrick-rodgers commented 6 years ago

Are you getting an error back? Some fields are not sortable and perhaps in the list you are accessing Title is not?

spvjebaraj commented 6 years ago

Hi Rodgers, I am not getting any error but it is not sorting. The results are displayed their own sorts not by ID or not by Title also. Regards, Philip

koltyakov commented 6 years ago

Hey Philip,

It's more likely something wrong with the code on your end. No one will argue that pagination in SP is a pain, but the getPaged method works well for me as it receives and uses skipToken prepared by API backend including edge values of the ordered by fields. For instance, .skip in this situation would return unpredictable sets of data while ordering by a field with non-unique values.

The image below shows paged results with filtering and ordering conditions:

image

spvjebaraj commented 6 years ago

Hi Andrew, It is working fine now. Thanks for shared the solution with good explanations and screenshots.

koltyakov commented 6 years ago

Nice! Glad you figured out. Closing the issue.