Elencare gli items di una lista con SharePoint Framework (SPFx)
Con lo SharePoint Framework (SPFx) è possibile accedere agli items di una lista tramite l'oggetto SPHttpClient:
per usarlo nella web part:
TypeScript
import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http';
export interface ILinkItem {
title: string;
url: string;
// altri campi
}
export default class SPDataService {
public getListItems(webAbsoluteUrl: string, listTitle: string): Promise<ILinkItem[]> {
//const url = this._webAbsoluteUrl + "/_api/Lists/getByTitle('" + listTitle + "')/items?$select=Title,Url";
const url = webAbsoluteUrl + "/_api/Lists/getByTitle('" + listTitle + "')/items?$select=Title,Url";
const items: ILinkItem[] = [];
return this._spHttpClient.get(url, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
return response.json().then((data) => {
if (data.error) {
const err = new Error(data.error.message);
console.log(err);
throw err;
}
data.value.forEach(item => {
const urlItem = item.URL;
items.push({
title: item.Title,
url: urlItem.Url,
// altri campi
});
});
return items;
});
});
}
}
TypeScript
import { IDataService, ILinkItem } from './data/SPDataService';
...
private _dataService: IDataService;
...
this._dataService = DataService.get(this.context);
...
this._dataService.getItems("/sites/miaSiteCollection/mioSito", "Mia Lista")
.then((items) => { ...
this.context.statusRenderer.clearLoadingIndicator(listContainer);
...
})
.catch((error) => {
this.context.statusRenderer.clearLoadingIndicator(listContainer);
this.context.statusRenderer.renderError(this.domElement, error);
});