I'm trying to create a custom list using @pnp/sp, in my routine I need to check if the list does exist, if it doesn't, them I will create the list and add the its columns.
The code below sometimes work, guess it is because sp.web.* methods are async, and it causes problems.
So, what is the correct way to 1) check for a particular list, 2) add the list if it doesn't exist, 3) add the fields to the list?
sp.web.lists.ensure("SliceBox").then( List => {
List.fields.getByTitle("Body").get().catch( f => {
f.fields.addMultilineText("Body", 4, true, false, false, true);
});
List.fields.getByTitle("Link").get().catch( f => {
f.fields.addUrl("Link", UrlFieldFormatType.Hyperlink);
});
List.fields.getByTitle("Visible").get().catch( f => {
f.fields.addBoolean("Visible");
});
})
.catch( err => {
console.log("> Failure: ", err);
});
Doesn't matter if I try the very explicit way (see below) it will also fail:
sp.web.lists.ensure("SliceBox").then( List => {
sp.web.lists.getByTitle("SliceBox").fields.getByTitle("Body").get().catch( f => {
f.fields.addMultilineText("Body", 4, true, false, false, true);
});
// ... shortened for brevity ...
})
.catch( err => {
console.log("> Failure: ", err);
});