Currently, I am attempting to create a custom list using @pnp/sp. In my process, it is necessary for me to verify if the list already exists. If it does not exist, then I will proceed to create the list and add its columns accordingly.
The issue lies in the fact that the code below only works sporadically. This inconsistency may be due to the asynchronous nature of sp.web.* methods, causing potential problems.
Therefore, what is the proper approach to 1) checking for a specific list, 2) creating the list if it is not found, and 3) adding 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);
});
Even attempting a more explicit method (as shown below) still encounters issues:
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);
});