Last active
March 1, 2018 09:27
-
-
Save wictorwilen/0bf866ee4fda2f9611f43ee17b9127d5 to your computer and use it in GitHub Desktop.
Simple SPFx ListService.ts
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
'use strict'; | |
import { HttpClient, HttpClientResponse } from '@microsoft/sp-http'; | |
import { IWebPartContext } from '@microsoft/sp-webpart-base'; | |
export interface IListsService { | |
getListNames(): Promise<string[]>; | |
getListColumns(listName: string): Promise<string[]>; | |
getListItems(listName: string, listColumns: string[]): Promise<string[][]> | |
} | |
export class MockListsService implements IListsService { | |
constructor() { | |
} | |
public getListNames(): Promise<string[]> { | |
return new Promise<string[]>(resolve => { | |
setTimeout(() => { | |
resolve(['List 1', 'List 2']); | |
}, 1000); | |
}); | |
} | |
public getListColumns(listName: string): Promise<string[]> { | |
return new Promise<string[]>(resolve => { | |
setTimeout(() => { | |
resolve([`${listName}:Column 1`, `${listName}:Column 2`, `${listName}:Column 3`]); | |
}, 1000); | |
}); | |
} | |
getListItems(listName: string, listColumns: string[]): Promise<string[][]> { | |
return new Promise<string[][]>(resolve => { | |
setTimeout(() => { | |
resolve( | |
[ | |
['A1', 'B1', 'C1'], | |
['A2', 'B2', 'C2'], | |
['A3', 'B3', 'C3'], | |
]); | |
}, 1000); | |
}); | |
} | |
} | |
export class ListsService implements IListsService { | |
private _httpClient: HttpClient; | |
private _webAbsoluteUrl: string; | |
public constructor(webPartContext: IWebPartContext) { | |
this._httpClient = webPartContext.httpClient as any; // tslint:disable-line:no-any | |
this._webAbsoluteUrl = webPartContext.pageContext.web.absoluteUrl; | |
} | |
public getListNames(): Promise<string[]> { | |
return this._httpClient.get(this._webAbsoluteUrl + `/_api/Lists/?$select=Title`, HttpClient.configurations.v1) | |
.then((response: HttpClientResponse) => { | |
var arr: string[] = []; | |
return response.json().then((data) => { | |
data.value.forEach(l => { | |
arr.push(l.Title); | |
}); | |
return arr; | |
}); | |
}); | |
} | |
public getListColumns(listName: string): Promise<string[]> { | |
return this._httpClient.get(this._webAbsoluteUrl + `/_api/Lists/GetByTitle('${listName}')/Fields?$Select=StaticName`, | |
HttpClient.configurations.v1) | |
.then((response: HttpClientResponse) => { | |
var arr: string[] = []; | |
return response.json().then((data) => { | |
data.value.forEach(l => { | |
arr.push(l.StaticName); | |
}); | |
return arr; | |
}); | |
}); | |
} | |
getListItems(listName: string, listColumns: string[]): Promise<string[][]> { | |
return this._httpClient.get(this._webAbsoluteUrl + `/_api/Lists/GetByTitle('${listName}')/Items?$Select=${encodeURI(listColumns.join(','))}`, | |
HttpClient.configurations.v1) | |
.then((response: HttpClientResponse) => { | |
var data: string[][] = [[]]; | |
return response.json().then((rows) => { | |
rows.value.forEach(row => { | |
var dataRow = []; | |
listColumns.forEach(c => { | |
dataRow.push(row[c]); | |
}); | |
data.push(dataRow); | |
}); | |
return data; | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment