Last active
November 25, 2022 04:12
-
-
Save gerald-magno/0c5077a75d571395572f871d6cc78ad9 to your computer and use it in GitHub Desktop.
SPFX - sample paging in sharepoint rest calls using $skiptoken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http'; | |
import { ICountryProjectsDataService } from "../ICountryProjectsDataService"; | |
import { ICountryProjects, IUser } from '../../../models'; | |
export class CountryProjectsDataService implements ICountryProjectsDataService { | |
//paging sharepoint rest api search results | |
public GetAllProjectsByCountryCode(spHttpClient:SPHttpClient, siteUrl:string, ctryCode:string, pageSize:number = 10, pageNumber:number = 1): Promise<ICountryPIViewModel[]> | |
{ | |
let baseList = "CountryProjects"; | |
//start of the current page | |
let startItemId:number = pageNumber > 1 ? (((pageNumber-1)*pageSize)) : 0; | |
let pagingQueryString = `$skiptoken=Paged%3DTRUE%26p_ID%3D${startItemId}&$top=${pageSize}`; | |
let filterQueryString = `$filter=Country/Code eq '${ctryCode}'`; | |
let selectQueryString = `$select=*,Country/Title`; | |
let expandQueryString = `$expand=Country`; | |
let refURL= `${siteUrl}/_api/web/lists/getbytitle('${baseList}')/items?${selectQueryString}&${filterQueryString}&${expandQueryString}&${pagingQueryString}`; | |
let data = spHttpClient.get(refURL, | |
SPHttpClient.configurations.v1, | |
{ | |
headers: { | |
'Accept': 'application/json;odata=nometadata', | |
'odata-version': '' | |
} | |
}) | |
.then((response: SPHttpClientResponse): Promise<{ value: ICountryProjects[] }> => { | |
return response.json(); | |
}) | |
.then((response: { value: any[] }): ICountryProjects[]=>{ | |
let countryProjItems: ICountryProjects[] = []; | |
if (response) | |
{ | |
for (let i = 0; i < response.value.length; i++) | |
{ | |
//do some operations here | |
//push item to collection | |
countryProjItems.push(listItem); | |
} | |
} | |
return countryProjItems; | |
}); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment