Skip to content

Commit f6c9752

Browse files
committed
Promise to async await
1 parent 3b67373 commit f6c9752

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

src/services/ISPService.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum LibsOrderBy {
44
Id = 1,
55
Title
66
}
7+
78
/**
89
* Options used to sort and filter
910
*/
@@ -12,16 +13,12 @@ export interface ILibsOptions {
1213
baseTemplate?: number;
1314
includeHidden?: boolean;
1415
}
16+
1517
export interface ISPService {
1618
/**
1719
* Get the lists from SharePoint
1820
* @param options Options used to order and filter during the API query
1921
*/
2022
getLibs(options?: ILibsOptions): Promise<ISPLists>;
21-
getListItems?(
22-
filterText: string,
23-
listId: string,
24-
internalColumnName: string,
25-
webUrl?: string
26-
) : Promise<any[]>;
23+
getListItems?(filterText: string, listId: string, internalColumnName: string, webUrl?: string) : Promise<any[]>;
2724
}

src/services/SPService.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ export default class SPService implements ISPService {
1313
});
1414
}
1515

16-
1716
/**
1817
* Get lists or libraries
1918
* @param options
2019
*/
21-
public getLibs(options?: ILibsOptions): Promise<ISPLists> {
20+
public async getLibs(options?: ILibsOptions): Promise<ISPLists> {
2221
let filtered: boolean;
2322
let queryUrl: string = `${this._context.pageContext.web.absoluteUrl}/_api/web/lists?$select=Title,id,BaseTemplate`;
2423

@@ -36,37 +35,30 @@ export default class SPService implements ISPService {
3635
filtered = true;
3736
}
3837

39-
return this._context.spHttpClient.get(queryUrl, SPHttpClient.configurations.v1)
40-
.then(response => response.json()) as Promise<ISPLists>;
38+
const data = await this._context.spHttpClient.get(queryUrl, SPHttpClient.configurations.v1);
39+
if (data.ok) {
40+
return await data.json() as Promise<ISPLists>;
41+
} else {
42+
return null;
43+
}
4144
}
4245

4346
/**
4447
* Get List Items
45-
*
4648
*/
47-
public async getListItems(
48-
filterText: string,
49-
listId: string,
50-
internalColumnName: string,
51-
webUrl?: string
52-
): Promise<any[]> {
49+
public async getListItems(filterText: string, listId: string, internalColumnName: string, webUrl?: string): Promise<any[]> {
5350
let filter = `startswith(${internalColumnName},'${filterText}')`;
51+
5452
let returnItems: any[];
55-
console.log(
56-
`Page context url ${this._context.pageContext.web.absoluteUrl}`
57-
);
5853
let spWeb: Web;
5954
if (typeof webUrl === undefined) {
6055
spWeb = new Web(webUrl);
6156
} else {
6257
spWeb = new Web(this._context.pageContext.web.absoluteUrl);
6358
}
59+
6460
try {
65-
returnItems = await spWeb.lists
66-
.getById(listId)
67-
.items.select("Id", internalColumnName)
68-
.filter(filter)
69-
.get();
61+
returnItems = await spWeb.lists.getById(listId).items.select("Id", internalColumnName).filter(filter).get();
7062
return Promise.resolve(returnItems);
7163
} catch (error) {
7264
return Promise.reject(error);

0 commit comments

Comments
 (0)