I am creating a list with specified columns on page load (later i will change this logic to on button click). I am using SPFx framework. I have installed sp-pnp libraries.
I am able to create the lists properly. I am creating two columns for each list. Out of 4 columns, 3 are creating successfully, but one is giving exception as The specified program requires a newer version of Windows. (Exception from HRESULT: 0x8007047E)
Below is my code: I have tried with both createFieldAsXml
and addText
private ValidateAndCreateLists() {
let listStructure: any = [{
"ListName": "Categories",
"ListTemplate":"100",
"Fields": [
"<Field DisplayName='My PnP TextField1' Type='Text' Required='FALSE' Name='mypnpfield1' />",
"<Field DisplayName='My PnP TextField2' Type='Text' Required='FALSE' Name='mypnpfield2' />"
],
"FieldTexts":["Test1", "Test2"]
},
{
"ListName": "ABC",
"ListTemplate":"100",
"Fields": [
"<Field DisplayName='My PnP TextField1' Type='Text' Required='FALSE' Name='mypnpfield1' />",
"<Field DisplayName='My PnP TextField2' Type='Text' Required='FALSE' Name='mypnpfield2' />"
],
"FieldTexts":["Test1", "Test2"]
}
];
Promise.all(listStructure.map((elm)=>{
return(this.CreateListInSP(elm));
}));
}
private CreateListInSP(element): Promise<any> {
let spWeb = new Web(this.context.pageContext.web.absoluteUrl);
let spListTitle = element["ListName"];
let spListTemplate = element["ListTemplate"];
let fieldsToCreate = element["FieldTexts"];
return spWeb.lists.add(spListTitle, "", spListTemplate, false).then((list) => {
return Promise.all(fieldsToCreate.map((elm)=>{
return(this.CreateColumnsInSPList(spListTitle,elm));
}));
//return Promise.resolve(true);
},()=>{
return Promise.resolve(false);
});
}
private CreateColumnsInSPList(listName,fields:string):Promise<any>{
let ctx = new Web(this.context.pageContext.web.absoluteUrl);
return (ctx.lists.getByTitle(listName).fields.addText(fields).then(()=>{
return Promise.resolve();
}));
}