private insertBulkItem(elements: any[]): Promise<any> {
return new Promise((resolve, reject) => {
if (elements.length > 0) {
// get first element of the array
const element = elements[0];
// remove the first element of the array
elements.shift();
pnp.sp.web.lists.getByTitle('Test').items.add(element).then(response => {
console.log('then...', response);
// call function again (first element removed before)
return this.insertBulkItem(elements);
}).catch(error => {
console.log('catch...', error);
reject(error);
});
} else {
// no (more) items in array
resolve(null);
}
});
}
// elements to add
// with EXACTLY the columns to set in SharePoint
const elements = [
{
Title: 'el1',
},
{
Title: 'el2',
},
{
Title: 'el3',
},
];
// add all the elements ordered
this.insertBulkItem(elements).then(response => {
// all added correctly
// do some stuff ...
console.log('success');
}).catch(error => {
// some error happened at some point...
// handle the error ...
console.log('something went wrong');
});
option with async and await
// call function
this.insertBulkItem(elements);
// async function with await
private async insertBulkItem(elements: any[]) {
for (const element of elements) {
await pnp.sp.web.lists.getByTitle('Test').items.add(element);
}
}
No comments:
Post a Comment